package com.mbbmap.map;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.StringUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.classes.CasemastFactory;
import com.classes.Report;
import com.mbbmap.app.home.SecUsersHome;
import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.security.action.CommonDispatchAction;
import com.mbbmap.security.dao.SecAccessDao;
import com.mbbmap.security.dao.SecGroupsDao;
import com.mbbmap.security.dao.SecUserDao;
import com.mbbmap.util.Constants;

public class ReportAction extends CommonDispatchAction {
	private int iApp6 = 0; //APP006
	private static final String CONFIG_FILE = "/system.properties";
	private static final java.util.Properties appCfg = new java.util.Properties();
	
	private boolean authorized(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		@SuppressWarnings("unchecked")
		ArrayList<SecAccessDao> secGroupAccessList = (ArrayList<SecAccessDao>) session.getAttribute(Constants.LOGON_ACCESS_LIST);
		
		this.iApp6 = 0;
		
		for(int i=0;i<secGroupAccessList.size();i++){
			SecAccessDao secaccessDao = (SecAccessDao)secGroupAccessList.get(i);
			String strModuleCode  = secaccessDao.getModuleCode();
			if(strModuleCode.equals("APP006")){
				this.iApp6 = 1;
			}
		}
		
		if(this.iApp6 > 0) {
			return true;
		}
		
		//return false;
		return true;
	}
	
	public ActionForward init(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		return sla_process(mapping, form, request, response);
	}
	
	public ActionForward terminal_installation(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.terminal_installation";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//			if(!isTokenValid(request)){
//				System.out.println("Token validation failed!");
//				session.invalidate();
//				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//			}else{
//				resetToken(request);
//				System.out.println("Token validation passed!");
//			}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		nextPage="terminal_installation_report";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
		
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
		}
		else {
			Date d = new Date();
			long t = d.getTime() - (24 * 60 * 60 * 1000);
			search.put("from_date", new SimpleDateFormat("dd/MM/yyyy").format(new Date(t)));
			search.put("to_date", new SimpleDateFormat("dd/MM/yyyy").format(new Date(t)));
		}
		
		
		ArrayList<TreeMap<String, String>> rArr = Report.getTerminalInstallation(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		request.setAttribute("rArr", rArr);
		request.setAttribute("search", search);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("ReportAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/terminal_installation_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
			
			int row_index = 0; 
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			
			/* ------------------------------------------------- Header Row --------------------------------------------- */
			CellStyle header_style = wb.createCellStyle();
			Font header_font = wb.createFont();
			header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			header_style.setFont(header_font);
			
			Row condition = sheet1.createRow(row_index);
			condition.createCell(1).setCellValue("Monthly");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(year);
			condition.createCell(3).setCellValue(month);
			
			condition = sheet1.createRow(++row_index);
			condition.createCell(1).setCellValue("Daily");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(from_date);
			condition.createCell(3).setCellValue(to_date);
			
			Row header = sheet1.createRow(++row_index);

			String[] header_array = {	"No.", 
										"MAP Reference Number", 
										"Vendor", 
										"Area", 
										"Job Type", 
										"Stock",
										"OIC",
										"Date Request Sent", 
										"Date Installed/Completed", 
										"SLA",
										"Appointment Date for Out of SLA Cases",
										"Status",
										"MBB Remarks/Job Request",
										"Wireless (Stock)",
										"DBT",
										"Merchant Name",
										"Address 1",
										"Address 2",
										"Address 3",
										"AMEX POS",
										"AMEX MOTO",
										"DINERS",
										"AEON",
										"Normal Acceptance",
										"Normal Acceptance",
										"Manual Key-In",
										"Manual Key-In",
										"Manual Key-In",
										"Manual Key-In",
										"Manual Key-In",
										"Manual Key-In",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Normal",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY Manual Key-In",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (POS)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"EZPAY - AMEX (MANUAL KEY-IN)",
										"Acceptance",
										"Acceptance",
										"Acceptance",
										"Acceptance",
										"Others",
										"Others",
										"Others",
										"Others",
										"Others",
										"Hotel Function",
										"Hotel Function",
										"Hotel Function",
										"Hotel Function",
										"Hotel Function",
										"Hotel Function"
									};
			
			Cell column = null;
			for(int i = 0; i < header_array.length; i++) {
				column = header.createCell(i);
				column.setCellValue(header_array[i]);
				column.setCellStyle(header_style);
			}
			
			row_index++;
			
			Row header2 = sheet1.createRow(row_index);

			String[] header_array2 = {	"No.", 
										"MAP Reference Number", 
										"Vendor", 
										"Area", 
										"Job Type", 
										"Stock",
										"OIC",
										"Date Request Sent", 
										"Date Installed/Completed", 
										"SLA",
										"Appointment Date for Out of SLA Cases",
										"Status",
										"MBB Remarks/Job Request",
										"Wireless (Stock)",
										"DBT",
										"Merchant Name",
										"Address 1",
										"Address 2",
										"Address 3",
										"AMEX POS",
										"AMEX MOTO",
										"DINERS",
										"AEON",
										"POS",
										"POS",
										"MO",
										"MO",
										"TO",
										"TO",
										"RPS",
										"RPS",
										"EZPAY 3MTH",
										"EZPAY 3MTH",
										"EZPAY 6MTH",
										"EZPAY 6MTH",
										"EZPAY 9MTH",
										"EZPAY 9MTH",
										"EZPAY 12MTH",
										"EZPAY 12MTH",
										"EZPAY 18MTH",
										"EZPAY 18MTH",
										"EZPAY 24MTH",
										"EZPAY 24MTH",
										"EZPAY 30MTH",
										"EZPAY 30MTH",
										"EZPAY 36MTH",
										"EZPAY 36MTH",
										"EZPAY 3MTH",
										"EZPAY 3MTH",
										"EZPAY 6MTH",
										"EZPAY 6MTH",
										"EZPAY 9MTH",
										"EZPAY 9MTH",
										"EZPAY 12MTH",
										"EZPAY 12MTH",
										"EZPAY 18MTH",
										"EZPAY 18MTH",
										"EZPAY 24MTH",
										"EZPAY 24MTH",
										"EZPAY 30MTH",
										"EZPAY 30MTH",
										"EZPAY 36MTH",
										"EZPAY 36MTH",
										"EZPAY 3MTH",
										"EZPAY 3MTH",
										"EZPAY 6MTH",
										"EZPAY 6MTH",
										"EZPAY 9MTH",
										"EZPAY 9MTH",
										"EZPAY 12MTH",
										"EZPAY 12MTH",
										"EZPAY 18MTH",
										"EZPAY 18MTH",
										"EZPAY 24MTH",
										"EZPAY 24MTH",
										"EZPAY 30MTH",
										"EZPAY 30MTH",
										"EZPAY 36MTH",
										"EZPAY 36MTH",
										"EZPAY 3MTH",
										"EZPAY 3MTH",
										"EZPAY 6MTH",
										"EZPAY 6MTH",
										"EZPAY 9MTH",
										"EZPAY 9MTH",
										"EZPAY 12MTH",
										"EZPAY 12MTH",
										"EZPAY 18MTH",
										"EZPAY 18MTH",
										"EZPAY 24MTH",
										"EZPAY 24MTH",
										"EZPAY 30MTH",
										"EZPAY 30MTH",
										"EZPAY 36MTH",
										"EZPAY 36MTH",
										"CUP",
										"ORS",
										"Airtime Reload",
										"VISA WAVE",
										"ECR Cable",
										"Disable Refer",
										"Cash Advance",
										"Cash Back",
										"Balance Inquiry",
										"Card Verify",
										"Offline",
										"Manual Key-In",
										"Tips",
										"Refund (Online)",
										"Refund (MKI)"
									};
			
			Cell column2 = null;
			for(int i = 0; i < header_array2.length; i++) {
				column2 = header2.createCell(i);
				column2.setCellValue(header_array2[i]);
				column2.setCellStyle(header_style);
			}
			
			row_index++;
			
			Row header3 = sheet1.createRow(row_index);

			String[] header_array3 = {	"No.", 
										"MAP Reference Number", 
										"Vendor", 
										"Area", 
										"Job Type", 
										"Stock",
										"OIC",
										"Date Request Sent", 
										"Date Installed/Completed", 
										"SLA",
										"Appointment Date for Out of SLA Cases",
										"Status",
										"MBB Remarks/Job Request",
										"Wireless (Stock)",
										"DBT",
										"Merchant Name",
										"Address 1",
										"Address 2",
										"Address 3",
										"AMEX POS",
										"AMEX MOTO",
										"DINERS",
										"AEON",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"TID",
										"MID",
										"CUP",
										"ORS",
										"Airtime Reload",
										"VISA WAVE",
										"ECR Cable",
										"Disable Refer",
										"Cash Advance",
										"Cash Back",
										"Balance Inquiry",
										"Card Verify",
										"Offline",
										"Manual Key-In",
										"Tips",
										"Refund (Online)",
										"Refund (MKI)"
									};
			
			Cell column3 = null;
			for(int i = 0; i < header_array3.length; i++) {
				column3 = header3.createCell(i);
				column3.setCellValue(header_array3[i]);
				column3.setCellStyle(header_style);
			}
			
			row_index++;
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			/* ------------------------------------------------------------ Result Row ------------------------------------- */
			Row row = null;
			
			int n = 0;
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) request.getAttribute("rArr")){
				JSONArray addrObj = new JSONArray();
				if(r.get("address") != null) {
					addrObj = (JSONArray) JSONValue.parse(r.get("address"));
				}
				
				JSONObject partnerObj = new JSONObject();
				if(r.get("partners") != null) {
					partnerObj = (JSONObject) JSONValue.parse(r.get("partners"));
				}
				
				JSONObject ezpayObj = new JSONObject();
				if(r.get("ezpay") != null) {
					ezpayObj = (JSONObject) JSONValue.parse(r.get("ezpay"));
				}
				
				JSONObject ezpayAmexObj = new JSONObject();
				if(r.get("ezpay_amex") != null) {
					ezpayAmexObj = (JSONObject) JSONValue.parse(r.get("ezpay_amex"));
				}
				
				JSONObject acceptanceObj = new JSONObject();
				if(r.get("acceptance") != null) {
					acceptanceObj = (JSONObject) JSONValue.parse(r.get("acceptance"));
				}
				
				JSONObject otherObj = new JSONObject();
				if(r.get("others") != null) {
					otherObj = (JSONObject) JSONValue.parse(r.get("others"));
				}
				
				JSONObject hotelFunctionObj = new JSONObject();
				if(r.get("hotel_function") != null) {
					hotelFunctionObj = (JSONObject) JSONValue.parse(r.get("hotel_function"));
				}
				
				JSONObject tempObj1 = (JSONObject) ezpayObj.get("E3");
				JSONObject tempObj2 = (JSONObject) ezpayObj.get("E6");
				JSONObject tempObj3 = (JSONObject) ezpayObj.get("E9");
				JSONObject tempObj4 = (JSONObject) ezpayObj.get("E12");
				JSONObject tempObj5 = (JSONObject) ezpayObj.get("E18");
				JSONObject tempObj6 = (JSONObject) ezpayObj.get("E24");
				JSONObject tempObj7 = (JSONObject) ezpayObj.get("E30");
				JSONObject tempObj8 = (JSONObject) ezpayObj.get("E36");
				
				JSONObject tempObj9 = (JSONObject) ezpayAmexObj.get("E3");
				JSONObject tempObj10 = (JSONObject) ezpayAmexObj.get("E6");
				JSONObject tempObj11 = (JSONObject) ezpayAmexObj.get("E9");
				JSONObject tempObj12 = (JSONObject) ezpayAmexObj.get("E12");
				JSONObject tempObj13 = (JSONObject) ezpayAmexObj.get("E18");
				JSONObject tempObj14 = (JSONObject) ezpayAmexObj.get("E24");
				JSONObject tempObj15 = (JSONObject) ezpayAmexObj.get("E30");
				JSONObject tempObj16 = (JSONObject) ezpayAmexObj.get("E36");
				
				row = sheet1.createRow(row_index);
				
				String[] result_array = {	Integer.toString(n+1), 
											(r.get("reference_no") != null ? r.get("reference_no") : ""), 
											(r.get("vendor") != null ? r.get("vendor") : ""), 
											(r.get("area") != null ? r.get("area") : ""), 
											(r.get("job_type") != null ? r.get("job_type") : ""), 
											(r.get("stock") != null ? r.get("stock") : ""), 
											(r.get("creator") != null ? r.get("creator") : ""), 
											(r.get("sent_date") != null ? r.get("sent_date") : ""),
											(r.get("completed_date") != null ? r.get("completed_date") : ""), 
											(r.get("sla") != null ? r.get("sla") : ""),
											(r.get("reschedule_date") != null ? r.get("reschedule_date") : ""), 
											(r.get("status") != null ? r.get("status") : ""), 
											(r.get("wireless_stock") != null ? r.get("wireless_stock") : ""), 
											(r.get("debit") != null ? r.get("debit") : ""), 
											(r.get("merchant_name") != null ? r.get("merchant_name") : ""), 
											(addrObj.get(0).toString() != null ? addrObj.get(0).toString() : ""), 
											(addrObj.get(1).toString() != null ? addrObj.get(1).toString() : ""),
											(addrObj.get(2).toString() != null ? addrObj.get(2).toString() : ""),
											(r.get("amex_pos") != null ? r.get("amex_pos") : ""), 
											(r.get("amex_moto") != null ? r.get("amex_moto") : ""), 
											(partnerObj.get("DINERS").toString() != null ? partnerObj.get("DINERS").toString() : ""), 
											(partnerObj.get("GENTING").toString() != null ? partnerObj.get("GENTING").toString() : ""), 
											(partnerObj.get("AEON").toString() != null ? partnerObj.get("AEON").toString() : ""), 
											(r.get("pos_tid") != null ? r.get("pos_tid") : ""), 
											(r.get("pos_mid") != null ? r.get("pos_mid") : ""),
											(tempObj1.get("tid_no").toString() != null ? tempObj1.get("tid_no").toString() : ""),
											(tempObj1.get("mid_no").toString() != null ? tempObj1.get("mid_no").toString() : ""),
											(tempObj2.get("tid_no").toString() != null ? tempObj2.get("tid_no").toString() : ""),
											(tempObj2.get("mid_no").toString() != null ? tempObj2.get("mid_no").toString() : ""),
											(tempObj3.get("tid_no").toString() != null ? tempObj3.get("tid_no").toString() : ""),
											(tempObj3.get("mid_no").toString() != null ? tempObj3.get("mid_no").toString() : ""),
											(tempObj4.get("tid_no").toString() != null ? tempObj4.get("tid_no").toString() : ""),
											(tempObj4.get("mid_no").toString() != null ? tempObj4.get("mid_no").toString() : ""),
											(tempObj5.get("tid_no").toString() != null ? tempObj5.get("tid_no").toString() : ""),
											(tempObj5.get("mid_no").toString() != null ? tempObj5.get("mid_no").toString() : ""),
											(tempObj6.get("tid_no").toString() != null ? tempObj6.get("tid_no").toString() : ""),
											(tempObj6.get("mid_no").toString() != null ? tempObj6.get("mid_no").toString() : ""),
											(tempObj7.get("tid_no").toString() != null ? tempObj7.get("tid_no").toString() : ""),
											(tempObj7.get("mid_no").toString() != null ? tempObj7.get("mid_no").toString() : ""),
											(tempObj8.get("tid_no").toString() != null ? tempObj8.get("tid_no").toString() : ""),
											(tempObj8.get("mid_no").toString() != null ? tempObj8.get("mid_no").toString() : ""),
											(tempObj9.get("tid_no").toString() != null ? tempObj9.get("tid_no").toString() : ""),
											(tempObj9.get("mid_no").toString() != null ? tempObj9.get("mid_no").toString() : ""),
											(tempObj10.get("tid_no").toString() != null ? tempObj10.get("tid_no").toString() : ""),
											(tempObj10.get("mid_no").toString() != null ? tempObj10.get("mid_no").toString() : ""),
											(tempObj11.get("tid_no").toString() != null ? tempObj11.get("tid_no").toString() : ""),
											(tempObj11.get("mid_no").toString() != null ? tempObj11.get("mid_no").toString() : ""),
											(tempObj12.get("tid_no").toString() != null ? tempObj12.get("tid_no").toString() : ""),
											(tempObj12.get("mid_no").toString() != null ? tempObj12.get("mid_no").toString() : ""),
											(tempObj13.get("tid_no").toString() != null ? tempObj13.get("tid_no").toString() : ""),
											(tempObj13.get("mid_no").toString() != null ? tempObj13.get("mid_no").toString() : ""),
											(tempObj14.get("tid_no").toString() != null ? tempObj14.get("tid_no").toString() : ""),
											(tempObj14.get("mid_no").toString() != null ? tempObj14.get("mid_no").toString() : ""),
											(tempObj15.get("tid_no").toString() != null ? tempObj15.get("tid_no").toString() : ""),
											(tempObj15.get("mid_no").toString() != null ? tempObj15.get("mid_no").toString() : ""),
											(tempObj16.get("tid_no").toString() != null ? tempObj16.get("tid_no").toString() : ""),
											(tempObj16.get("mid_no").toString() != null ? tempObj16.get("mid_no").toString() : ""),
											(acceptanceObj.get("UPI").toString() != null ? acceptanceObj.get("UPI").toString() : ""), 
											(acceptanceObj.get("ORS").toString() != null ? acceptanceObj.get("ORS").toString() : ""),
											(acceptanceObj.get("DCC").toString() != null ? acceptanceObj.get("DCC").toString() : ""),
											(acceptanceObj.get("WAVE").toString() != null ? acceptanceObj.get("WAVE").toString() : ""),
											(otherObj.get("ECR Cable").toString() != null ? otherObj.get("ECR Cable").toString() : ""), 
											(otherObj.get("Cash Advance").toString() != null ? otherObj.get("Cash Advance").toString() : ""),
											(otherObj.get("Cash Back").toString() != null ? otherObj.get("Cash Back").toString() : ""),
											(hotelFunctionObj.get("Card Verify").toString() != null ? hotelFunctionObj.get("Card Verify").toString() : ""),
											(hotelFunctionObj.get("Offline").toString() != null ? hotelFunctionObj.get("Offline").toString() : ""),
											(hotelFunctionObj.get("Manual Key-In").toString() != null ? hotelFunctionObj.get("Manual Key-In").toString() : ""),
											(hotelFunctionObj.get("Tips").toString() != null ? hotelFunctionObj.get("Tips").toString() : ""),
											(hotelFunctionObj.get("Refund").toString() != null ? hotelFunctionObj.get("Refund").toString() : ""),
											(r.get("remark") != null ? r.get("remark") : ""), 
										};
				
				Cell result_column = null;
				for(int i = 0; i < result_array.length; i++) {
					result_column = row.createCell(i);
					result_column.setCellValue(result_array[i]);
				}
				
				row_index++;
				n++;
				
			}
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"Terminal Installation.xls\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
	
	public ActionForward terminal_summary(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.terminal_summary";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		nextPage="terminal_summary_report";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
		
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
		}
		else {
			Date d = new Date();
			long t = d.getTime() - (24 * 60 * 60 * 1000);
			search.put("from_date", new SimpleDateFormat("dd/MM/yyyy").format(new Date(t)));
			search.put("to_date", new SimpleDateFormat("dd/MM/yyyy").format(new Date(t)));
		}
		
		
		ArrayList<TreeMap<String, String>> rArr = Report.getTerminalInstallation(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		request.setAttribute("rArr", rArr);
		request.setAttribute("search", search);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/terminal_summary_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
			
			int row_index = 0;
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));

			
			/* ------------------------------------------------- Header Row --------------------------------------------- */
			CellStyle header_style = wb.createCellStyle();
			Font header_font = wb.createFont();
			header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			header_style.setFont(header_font);
			

			Row condition = sheet1.createRow(row_index);
			condition.createCell(1).setCellValue("Monthly");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(year);
			condition.createCell(3).setCellValue(month);
			
			condition = sheet1.createRow(++row_index);
			condition.createCell(1).setCellValue("Daily");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(from_date);
			condition.createCell(3).setCellValue(to_date);
			
			Row header = sheet1.createRow(++row_index);

			String[] header_array = {	"No.", 
										"MAP Reference Number", 
										"Merchant Name", 
										"TID", 
										"MAP Serial No.", 
										"Date Request Received",
										"Date Request Sent",
										"Vendor", 
										"TMU Staff Name", 
										"Status"
									};
			
			Cell column = null;
			for(int i = 0; i < header_array.length; i++) {
				column = header.createCell(i);
				column.setCellValue(header_array[i]);
				column.setCellStyle(header_style);
			}
			
			row_index++;
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			/* ------------------------------------------------------------ Result Row ------------------------------------- */
			Row row = null;
			
			int n = 0;
			for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("rArr")){
				row = sheet1.createRow(row_index);
				
				String[] result_array = {	Integer.toString(n+1), 
											(c.get("reference_no") != null ? c.get("reference_no") : ""), 
											(c.get("merchant_name") != null ? c.get("merchant_name") : ""), 
											(c.get("pos_tid") != null ? c.get("pos_tid") : ""), 
											(c.get("serial_no") != null ? c.get("serial_no") : ""), 
											"", 
											(c.get("sent_date") != null ? c.get("sent_date") : ""), 
											(c.get("vendor") != null ? c.get("vendor") : ""),
											(c.get("creator") != null ? c.get("creator") : ""), 
											(c.get("status") != null ? c.get("status") : "")
										};
				
				Cell result_column = null;
				for(int i = 0; i < result_array.length; i++) {
					result_column = row.createCell(i);
					result_column.setCellValue(result_array[i]);
				}
				
				row_index++;
				n++;
				
			}
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"Terminal Summary Report.xls\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		return mapping.findForward(nextPage);
	}
	
	public ActionForward trf_report(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.trf_report";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="trf_report";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
			
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_user = sanitize(request.getParameter("group_user"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
			
			if(request.getParameter("group_code") != null){
				search.put("group_code", group_code);
			}
			
			if(request.getParameter("group_user") != null){
				search.put("group_user", group_user);
			}
		}
		
		ArrayList<TreeMap<String, String>> rArr = Report.getTRF(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		request.setAttribute("rArr", rArr);
		request.setAttribute("search", search);
		
		ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			if(search.containsKey("group_code") && search.get("group_code").length() > 0){
				userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, search.get("group_code"));
			}
		}
		
		request.setAttribute("userList", userList);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/trf_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
			
			int row_index = 0; 
			
			/* ------------------------------------------------- Header Row --------------------------------------------- */
			CellStyle header_style = wb.createCellStyle();
			Font header_font = wb.createFont();
			header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			header_style.setFont(header_font);
			
			Row header = sheet1.createRow(row_index);

			String[] header_array = {	"No.", 
										"MAP Reference Number", 
										"OIC", 
										"Merchant Register Name", 
										"MID", 
										"TID",
										"Wired/Wireless",
										"Vendor", 
										"Account No.", 
										"Bus Reg.",
										"Monthly Rental Fee",
										"Installation Date",
										"Cheque No.",
										"Cheque Date",
										"Bank-Branch",
										"Amount",
										"Adv. Amount",
										"1st Rental Fee",
										"Remarks"
									};
			
			Cell column = null;
			for(int i = 0; i < header_array.length; i++) {
				column = header.createCell(i);
				column.setCellValue(header_array[i]);
				column.setCellStyle(header_style);
			}
			
			row_index++;
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			/* ------------------------------------------------------------ Result Row ------------------------------------- */
			Row row = null;
			
			int n = 0;
			for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("rArr")){
				row = sheet1.createRow(row_index);
				
				String[] result_array = {	Integer.toString(n+1), 
											(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
											(c.get("oic") != null ? c.get("oic") : ""), 
											(c.get("merchant_name") != null ? c.get("merchant_name") : ""), 
											(c.get("mid") != null ? c.get("mid") : ""), 
											(c.get("tid") != null ? c.get("tid") : ""), 
											(c.get("wired_wireless") != null ? c.get("wired_wireless") : ""), 
											(c.get("vendor") != null ? c.get("vendor") : ""),
											(c.get("account") != null ? c.get("account") : ""), 
											(c.get("bus_reg") != null ? c.get("bus_reg") : ""),
											(c.get("rental") != null ? c.get("rental") : ""), 
											(c.get("install_date") != null ? c.get("install_date") : ""), 
											(c.get("cheque_no") != null ? c.get("cheque_no") : ""), 
											(c.get("cheque_date") != null ? c.get("cheque_date") : ""), 
											(c.get("bank_branch") != null ? c.get("bank_branch") : ""), 
											(c.get("amount") != null ? c.get("amount") : ""), 
											(c.get("adv_amount") != null ? c.get("adv_amount") : ""),
											(c.get("first_rental") != null ? c.get("first_rental") : "")
										};
				
				Cell result_column = null;
				for(int i = 0; i < result_array.length; i++) {
					result_column = row.createCell(i);
					result_column.setCellValue(result_array[i]);
				}
				
				row_index++;
				n++;
				
			}
			/* --------------------------------------------------------------------------------------------------------------*/
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"TRF Report.xls\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
		
	public ActionForward sla_process(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.sla_process";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//			if(!isTokenValid(request)){
//				System.out.println("Token validation failed!");
//				session.invalidate();
//				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//			}else{
//				resetToken(request);
//				System.out.println("Token validation passed!");
//			}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="sla_process";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
			
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_user = sanitize(request.getParameter("group_user"));
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
			
			if(request.getParameter("group_code") != null){
				search.put("group_code", group_code);
			}
			
			if(request.getParameter("group_user") != null){
				search.put("group_user", group_user);
			}
			
			if(request.getParameter("application_type") != null){
				search.put("application_type", application_type);
			}
			
			if(request.getParameter("reseller_location") != null){
				search.put("reseller_location", reseller_location);
			}
		}
		
		System.out.println("Search Filter --> "  + search.toString());
		ArrayList<TreeMap<String, String>> rArr = Report.getSlaProcess(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		System.out.println("Result --> " + rArr.toString());
		
		/* Getting SLA limit of days */
		try {
			InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
			if (stream != null){
				appCfg.load(stream);
			} else {
				System.out.println("ReportAction Input Stream is NULL");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int sla_days = Integer.parseInt(appCfg.getProperty("sla_days"));
		
		/*TreeMap<String, String> summary = Report.getSlaProcessSummary(search, sla_days);
		if(summary == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		System.out.println("Summary --> " + summary.toString());
		
		
		if(request.getParameter("search_submit") != null){
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			
			if(request.getParameter("month") != null){
				search.put("month", month);
			}
			
			if(request.getParameter("year") != null){
				search.put("year", year);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "");
				search.put("to_date", "");
			}
		}*/
		
		TreeMap<String, String> summaryMatrix = Report.getSlaProcessSummary(search, sla_days, "Y");
		if(summaryMatrix == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		TreeMap<String, String> summaryNonMatrix = Report.getSlaProcessSummary(search, sla_days, "N");
		if(summaryNonMatrix == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		TreeMap<String, String> summaryOther = Report.getSlaProcessSummary(search, sla_days, null);
		if(summaryOther == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		
		request.setAttribute("search", search);
		request.setAttribute("rArr", rArr);


		request.setAttribute("summaryMatrix", summaryMatrix);
		request.setAttribute("summaryNonMatrix", summaryNonMatrix);
		request.setAttribute("summaryOther", summaryOther);
		
		
		ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			if(search.containsKey("group_code") && search.get("group_code").length() > 0){
				userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, search.get("group_code"));
			}
		}
		
		request.setAttribute("userList", userList);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/sla_process_" + oSecUserDao.getLogonId() + "_" + sdf.format(new Date()) + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
		    
			CellStyle style = wb.createCellStyle();//Create style
		    Font font = wb.createFont();//Create font
		    font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
		    style.setFont(font);//set it to bold
		    
			Row row = sheet1.createRow(0);
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_name = Report.getGroupName(group_code);
			String group_user = sanitize(request.getParameter("group_user"));
			String group_user_name = Report.getUserName(group_user);
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));
			
			row.createCell(1).setCellValue("Application Type");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(application_type);
			
			row.createCell(6).setCellValue("Group");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_name);
			
			row = sheet1.createRow(1);
			row.createCell(1).setCellValue("Period");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(from_date);
			row.createCell(3).setCellValue("To");
			row.getCell(3).setCellStyle(style);
			row.createCell(4).setCellValue(to_date);
			
			row.createCell(6).setCellValue("User");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_user_name);
			
			row= sheet1.createRow(2);
			row.createCell(1).setCellValue("Month");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(month);
			row.createCell(3).setCellValue(year);
			row.createCell(6).setCellValue("Region");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(reseller_location);
			
			row = sheet1.createRow(3);
			row.createCell(0).setCellValue("No.");
			row.createCell(1).setCellValue("MAP Reference No.");
			row.createCell(2).setCellValue("Application Type");
			row.createCell(3).setCellValue("Matrix & Non Matrix Transition");
			row.createCell(4).setCellValue("Group Name");
			row.createCell(5).setCellValue("Creator");
			row.createCell(6).setCellValue("Region (Maxis)");
			row.createCell(7).setCellValue("Process 1: Identify and Sourcing for Merchant. Merchant Submit Application Form");
			row.createCell(8).setCellValue("SLA (no of days)");
			row.createCell(9).setCellValue("Process 2: Capture Application Data, Scan Supporting Docs & Submit Application Form");
			row.createCell(10).setCellValue("SLA (no of days)");
			row.createCell(11).setCellValue("Process 3: Internal & External Financial Checking");
			row.createCell(12).setCellValue("SLA (no of days)");
			row.createCell(13).setCellValue("Process 4: Decisioning");
			row.createCell(14).setCellValue("SLA (no of days)");
			row.createCell(15).setCellValue("Process 4a: Merchant Acceptance");
			row.createCell(16).setCellValue("SLA (no of days)");
			row.createCell(17).setCellValue("Process 5: Validation & Verification Cardlink Setup");
			row.createCell(18).setCellValue("SLA (no of days)");
			row.createCell(19).setCellValue("Process 5a: Verification with Barcode Update");
			row.createCell(20).setCellValue("SLA (no of days)");
			row.createCell(21).setCellValue("Process 6: Activiate TID & Notify Termnial Provider");
			row.createCell(22).setCellValue("SLA (no of days)");
			row.createCell(23).setCellValue("Process 7: Terminal Installation & Training");
			row.createCell(24).setCellValue("SLA (no of days)");
			row.createCell(25).setCellValue("TAT");
		
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 0, 0));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 1, 1));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 2, 2));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 3, 3));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 4, 4));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 5, 5));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 6, 6));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 7, 7));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 8, 8));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 9, 9));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 10, 10));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 11, 11));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 12, 12));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 13, 13));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 14, 14));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 15, 15));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 16, 16));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 17, 17));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 18, 18));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 19, 19));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 20, 20));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 21, 21));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 22, 22));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 23, 23));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 24, 24));
			sheet1.addMergedRegion(new CellRangeAddress(3, 3, 25, 28));

			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			row = sheet1.createRow(5);
			row.createCell(25).setCellValue("Current");
			row.createCell(26).setCellValue("End to end");
			row.createCell(27).setCellValue("Current Exclude 4a: Merchant Acceptance");
			row.createCell(28).setCellValue("Current Exclude 5a: Verification with Barcode Update");

		    for(int i = 25; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			    
			int n = 1;
			int process_1 = 0; double process_1C = 0.0;
			int process_2 = 0; double process_2C = 0.0;
			int process_3 = 0; double process_3C = 0.0;
			int process_4 = 0; double process_4C = 0.0;
			int process_4a = 0; double process_4aC = 0.0;
			int process_5 = 0; double process_5C = 0.0;
			int process_5a = 0; double process_5aC = 0.0;
			int process_6 = 0; double process_6C = 0.0;
			int process_7 = 0; double process_7C = 0.0;
			int current_sla = 0; double current_slaC = 0.0;
			int end_to_end_sla = 0; double end_to_end_slaC = 0.0;
			int current_4a = 0; double current_4aC = 0.0;
			int current_5a = 0; double current_5aC = 0.0;
			int rowNo = 6;
			int colNo = 0;
			
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) rArr){
				String iStatuses[] = {
						r.get("status_1"), r.get("status_2"), r.get("status_3"), r.get("status_4")
						, r.get("status_4a"), r.get("status_5"), r.get("status_5a"), r.get("status_6"), r.get("status_7")
					};
					String iResult [] = new String [9];
					iResult[0] =  (r.get("status_1") == null? "-" : r.get("status_1")); 
					iResult[8] =  (r.get("status_7") == null? "-" : r.get("status_7"));
					
					for(int i = iStatuses.length - 2; i > 0; i--){
						if(iStatuses[i] != null){
							iResult[i] = iStatuses[i] ;
						}else{
							if("-".equals(iResult[i + 1])){
								iResult[i] = "-";
							}else{
								for(int j = 0; j < i; j++){
									if(iStatuses[j] != null){
										iResult[i] = "N/A";
										break;
									}
									iResult[i] = "-";
								}
							}
						}
					}
				
				if(r.get("process_1") != null) {
					process_1C++;
					process_1 += Integer.parseInt(r.get("process_1"));
				}
				
				if(r.get("process_2") != null) {
					process_2C++;
					process_2 += Integer.parseInt(r.get("process_2"));
				}
				
				if(r.get("process_3") != null) {
					process_3C++;
					process_3 += Integer.parseInt(r.get("process_3"));
				}
				
				if(r.get("process_4") != null) {
					process_4C++;
					process_4 += Integer.parseInt(r.get("process_4"));
				}
				
				if(r.get("process_4a") != null) {
					process_4aC++;
					process_4a += Integer.parseInt(r.get("process_4a"));
				}
				
				if(r.get("process_5") != null) {
					process_5C++;
					process_5 += Integer.parseInt(r.get("process_5"));
				}
				
				if(r.get("process_5a") != null) {
					process_5aC++;
					process_5a += Integer.parseInt(r.get("process_5a"));
				}
				
				if(r.get("process_6") != null) {
					process_6C++;
					process_6 += Integer.parseInt(r.get("process_6"));
				}
				
				if(r.get("process_7") != null) {
					process_7C++;
					process_7 += Integer.parseInt(r.get("process_7"));
				}
				
				if(r.get("current_sla") != null) {
					current_slaC++;
					current_sla += Integer.parseInt(r.get("current_sla"));
				}
				
				if(r.get("end_to_end_sla") != null) {
					end_to_end_slaC++;
					end_to_end_sla += Integer.parseInt(r.get("end_to_end_sla"));
				}
				
				if(r.get("current_4a") != null) {
					current_4aC++;
					current_4a += Integer.parseInt(r.get("current_4a"));
				}
				
				if(r.get("current_5a") != null) {
					current_5aC++;
					current_5a += Integer.parseInt(r.get("current_5a"));
				}
				
				row = sheet1.createRow(rowNo++);
				colNo = 0;
				
				row.createCell(colNo++).setCellValue(n++);
				row.createCell(colNo++).setCellValue(r.get("reference_no") == null ? "-" : r.get("reference_no"));
				row.createCell(colNo++).setCellValue(r.get("type") == null ? "-" : r.get("type"));
				row.createCell(colNo++).setCellValue(r.get("matrix") == null ? "-" : r.get("matrix"));
				row.createCell(colNo++).setCellValue(r.get("group_name") == null ? "-" : r.get("group_name"));
				row.createCell(colNo++).setCellValue(r.get("creator") == null ? "-" : r.get("creator"));
				row.createCell(colNo++).setCellValue(r.get("region") == null ? "-" : r.get("region"));
			/*	row.createCell(colNo++).setCellValue(r.get("status_1") == null ? "-" : r.get("status_1"));
				row.createCell(colNo++).setCellValue(r.get("process_1") == null ? "-" : r.get("process_1"));
				row.createCell(colNo++).setCellValue(r.get("status_2") == null ? "-" : r.get("status_2"));
				row.createCell(colNo++).setCellValue(r.get("process_2") == null ? "-" : r.get("process_2"));
				row.createCell(colNo++).setCellValue(r.get("status_3") == null ? "-" : r.get("status_3"));
				row.createCell(colNo++).setCellValue(r.get("process_3") == null ? "-" : r.get("process_3"));
				row.createCell(colNo++).setCellValue(r.get("status_4") == null ? "-" : r.get("status_4"));
				row.createCell(colNo++).setCellValue(r.get("process_4") == null ? "-" : r.get("process_4"));
				row.createCell(colNo++).setCellValue(r.get("status_4a") == null ? "-" : r.get("status_4a"));
				row.createCell(colNo++).setCellValue(r.get("process_4a") == null ? "-" : r.get("process_4a"));
				row.createCell(colNo++).setCellValue(r.get("status_5") == null ? "-" : r.get("status_5"));
				row.createCell(colNo++).setCellValue(r.get("process_5") == null ? "-" : r.get("process_5"));
				row.createCell(colNo++).setCellValue(r.get("status_5a") == null ? "-" : r.get("status_5a"));
				row.createCell(colNo++).setCellValue(r.get("process_5a") == null ? "-" : r.get("process_5a"));
				row.createCell(colNo++).setCellValue(r.get("status_6") == null ? "-" : r.get("status_6"));
				row.createCell(colNo++).setCellValue(r.get("process_6") == null ? "-" : r.get("process_6"));
				row.createCell(colNo++).setCellValue(r.get("status_7") == null ? "-" : r.get("status_7"));
				row.createCell(colNo++).setCellValue(r.get("process_7") == null ? "-" : r.get("process_7"));
				row.createCell(colNo++).setCellValue(r.get("current_sla") == null ? "-" : r.get("current_sla"));
				row.createCell(colNo++).setCellValue(r.get("end_to_end_sla") == null ? "-" : r.get("end_to_end_sla"));
				row.createCell(colNo++).setCellValue(r.get("current_4a") == null ? "-" : r.get("current_4a"));
				row.createCell(colNo++).setCellValue(r.get("current_5a") == null ? "-" : r.get("current_5a"));*/
				
				row.createCell(colNo++).setCellValue(iResult[0]);
				row.createCell(colNo++).setCellValue(r.get("process_1") == null ? iResult[0] : r.get("process_1"));
				row.createCell(colNo++).setCellValue(iResult[1]);
				row.createCell(colNo++).setCellValue(r.get("process_2") == null ? iResult[1] : r.get("process_2"));
				row.createCell(colNo++).setCellValue(iResult[2]);
				row.createCell(colNo++).setCellValue(r.get("process_3") == null ? iResult[2] : r.get("process_3"));
				row.createCell(colNo++).setCellValue(iResult[3]);
				row.createCell(colNo++).setCellValue(r.get("process_4") == null ? iResult[3] : r.get("process_4"));
				row.createCell(colNo++).setCellValue(iResult[4]);
				row.createCell(colNo++).setCellValue(r.get("process_4a") == null ? iResult[4] : r.get("process_4a"));
				row.createCell(colNo++).setCellValue(iResult[5]);
				row.createCell(colNo++).setCellValue(r.get("process_5") == null ? iResult[5] : r.get("process_5"));
				row.createCell(colNo++).setCellValue(iResult[6]);
				row.createCell(colNo++).setCellValue(r.get("process_5a") == null ? iResult[6] : r.get("process_5a"));
				row.createCell(colNo++).setCellValue(iResult[7]);
				row.createCell(colNo++).setCellValue(r.get("process_6") == null ? iResult[7] : r.get("process_6"));
				row.createCell(colNo++).setCellValue(iResult[8]);
				row.createCell(colNo++).setCellValue(r.get("process_7") == null ? iResult[8] : r.get("process_7"));
				row.createCell(colNo++).setCellValue(r.get("current_sla") == null ? "-" : r.get("current_sla"));
				row.createCell(colNo++).setCellValue(r.get("end_to_end_sla") == null ? "-" : r.get("end_to_end_sla"));
				row.createCell(colNo++).setCellValue(r.get("current_4a") == null ? "-" : r.get("current_4a"));
				row.createCell(colNo++).setCellValue(r.get("current_5a") == null ? "-" : r.get("current_5a"));
			}
			
			row = sheet1.createRow(rowNo++);
			colNo = 0;
			
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_1C > 0 ? String.format("%.2f", process_1/process_1C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_2C > 0 ? String.format("%.2f", process_2/process_2C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_3C > 0 ? String.format("%.2f", process_3/process_3C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_4C > 0 ? String.format("%.2f", process_4/process_4C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_4aC > 0 ? String.format("%.2f", process_4a/process_4aC) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_5C > 0 ? String.format("%.2f", process_5/process_5C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_5aC > 0 ? String.format("%.2f", process_5a/process_5aC) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_6C > 0 ? String.format("%.2f", process_6/process_6C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_7C > 0 ? String.format("%.2f", process_7/process_7C) : "-"));
			row.createCell(colNo++).setCellValue((current_slaC > 0 ? String.format("%.2f", current_sla/current_slaC) : "-"));
			row.createCell(colNo++).setCellValue((end_to_end_slaC > 0 ? String.format("%.2f", end_to_end_sla/end_to_end_slaC) : "-"));
			row.createCell(colNo++).setCellValue((current_4aC > 0 ? String.format("%.2f", current_4a/current_4aC) : "-"));
			row.createCell(colNo++).setCellValue((current_5aC > 0 ? String.format("%.2f", current_5a/current_5aC) : "-"));
			
			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			
			//Matrix
			row.createCell(1).setCellValue("Matrix");
			row.getCell(1).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("i) Completed = " + (summaryMatrix.get("completed") == null ? "-" : summaryMatrix.get("completed")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("ii) Work in Progress = " + (summaryMatrix.get("pending") == null ? "-" : summaryMatrix.get("pending")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iii) Unsuccessful = " + (summaryMatrix.get("unsuccessful") == null ? "-" : summaryMatrix.get("unsuccessful")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iv) Total= " + (summaryMatrix.get("total") == null ? "-" : summaryMatrix.get("total")));
			
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Description");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue("KV (6 working days)");
			row.getCell(2).setCellStyle(style);
			row.createCell(3).setCellValue("OS (7 working days)");
			row.getCell(3).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Total Application within SLA ");
			row.createCell(2).setCellValue((summaryMatrix.get("completed_1_KV") == null ? "-" : summaryMatrix.get("completed_1_KV")));
			row.createCell(3).setCellValue((summaryMatrix.get("completed_1_OS") == null ? "-" : summaryMatrix.get("completed_1_OS")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Total Application not within SLA ");
			row.createCell(2).setCellValue((summaryMatrix.get("completed_2_KV") == null ? "-" : summaryMatrix.get("completed_2_KV")));
			row.createCell(3).setCellValue((summaryMatrix.get("completed_2_OS") == null ? "-" : summaryMatrix.get("completed_2_OS")));
			
			//Non Matrix
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			
			row.createCell(1).setCellValue("Non Matrix");
			row.getCell(1).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("i) Completed = " + (summaryNonMatrix.get("completed") == null ? "-" : summaryNonMatrix.get("completed")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("ii) Work in Progress = " + (summaryNonMatrix.get("pending") == null ? "-" : summaryNonMatrix.get("pending")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iii) Unsuccessful = " + (summaryNonMatrix.get("unsuccessful") == null ? "-" : summaryNonMatrix.get("unsuccessful")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iv) Total= " + (summaryNonMatrix.get("total") == null ? "-" : summaryNonMatrix.get("total")));
			
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Description");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue("KV (6 working days)");
			row.getCell(2).setCellStyle(style);
			row.createCell(3).setCellValue("OS (7 working days)");
			row.getCell(3).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Total Application within SLA ");
			row.createCell(2).setCellValue((summaryNonMatrix.get("completed_1_KV") == null ? "-" : summaryNonMatrix.get("completed_1_KV")));
			row.createCell(3).setCellValue((summaryNonMatrix.get("completed_1_OS") == null ? "-" : summaryNonMatrix.get("completed_1_OS")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Total Application not within SLA ");
			row.createCell(2).setCellValue((summaryNonMatrix.get("completed_2_KV") == null ? "-" : summaryNonMatrix.get("completed_2_KV")));
			row.createCell(3).setCellValue((summaryNonMatrix.get("completed_2_OS") == null ? "-" : summaryNonMatrix.get("completed_2_OS")));
			
			//Other
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			
			row.createCell(1).setCellValue("Other");
			row.getCell(1).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("i) Completed = " + (summaryOther.get("completed") == null ? "-" : summaryOther.get("completed")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("ii) Work in Progress = " + (summaryOther.get("pending") == null ? "-" : summaryOther.get("pending")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iii) Unsuccessful = " + (summaryOther.get("unsuccessful") == null ? "-" : summaryOther.get("unsuccessful")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iv) Total= " + (summaryOther.get("total") == null ? "-" : summaryOther.get("total")));
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileToDownload.getName() + "\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
		
	public ActionForward external_sla_process(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.external_sla_process";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="external_sla_process";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
			
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_user = sanitize(request.getParameter("group_user"));
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
			
			if(request.getParameter("group_code") != null){
				search.put("group_code", group_code);
			}
			
			if(request.getParameter("group_user") != null){
				search.put("group_user", group_user);
			}
			
			if(request.getParameter("application_type") != null){
				search.put("application_type", application_type);
			}
			
			if(request.getParameter("reseller_location") != null){
				search.put("reseller_location", reseller_location);
			}
		}
		
		System.out.println("Search Filter --> "  + search.toString());
		ArrayList<TreeMap<String, String>> rArr = Report.getExternalSlaProcess(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		System.out.println("Result --> " + rArr.toString());
		
		
		/* Getting SLA limit of days */
		try {
			InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
			if (stream != null){
				appCfg.load(stream);
			} else {
				System.out.println("ReportAction Input Stream is NULL");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int sla_days = Integer.parseInt(appCfg.getProperty("sla_days"));
		
		TreeMap<String, String> summary = Report.getExternalSlaProcessSummary(search, sla_days);
		if(summary == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		System.out.println("Summary --> " + summary.toString());
		
		
		if(request.getParameter("search_submit") != null){
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			
			if(request.getParameter("month") != null){
				search.put("month", month);
			}
			
			if(request.getParameter("year") != null){
				search.put("year", year);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "");
				search.put("to_date", "");
			}
		}
		
		request.setAttribute("search", search);
		request.setAttribute("rArr", rArr);


		request.setAttribute("summary", summary);
		
		
		ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			if(search.containsKey("group_code") && search.get("group_code").length() > 0){
				userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, search.get("group_code"));
			}
		}
		
		request.setAttribute("userList", userList);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/external_sla_process_" + oSecUserDao.getLogonId() + "_" + sdf.format(new Date()) + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_name = Report.getGroupName(group_code);
			String group_user = sanitize(request.getParameter("group_user"));
			String group_user_name = Report.getUserName(group_user);
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));
			
			/*----------------------------------------- HEADER --------------------------------------------------*/
		    
			CellStyle style = wb.createCellStyle();//Create style
		    Font font = wb.createFont();//Create font
		    font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
		    style.setFont(font);//set it to bold
		    
		    Row row = sheet1.createRow(0);
			row.createCell(1).setCellValue("Application Type");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(application_type);
			
			row.createCell(6).setCellValue("Group");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_name);
			
			row = sheet1.createRow(1);
			row.createCell(1).setCellValue("Period");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(from_date);
			row.createCell(3).setCellValue("To");
			row.getCell(3).setCellStyle(style);
			row.createCell(4).setCellValue(to_date);
			
			row.createCell(6).setCellValue("User");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_user_name);
			
			row= sheet1.createRow(2);
			row.createCell(1).setCellValue("Month");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(month);
			row.createCell(3).setCellValue(year);
			row.createCell(6).setCellValue("Region");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(reseller_location);
			
			row = sheet1.createRow(3);

			row.createCell(0).setCellValue("No.");
			row.createCell(1).setCellValue("MAP Reference No.");
			row.createCell(2).setCellValue("Application Type");
			row.createCell(3).setCellValue("Matrix & Non Matrix Transition");
			row.createCell(4).setCellValue("Group Name");
			row.createCell(5).setCellValue("Creator");
			row.createCell(6).setCellValue("Region (Maxis)");
			row.createCell(7).setCellValue("Process 1: Identify and Sourcing for Merchant. Merchant Submit Application Form");
			row.createCell(8).setCellValue("SLA (no of days)");
			row.createCell(9).setCellValue("Process 2: Capture Application Data, Scan Supporting Docs & Submit Application Form");
			row.createCell(10).setCellValue("SLA (no of days)");
			row.createCell(11).setCellValue("Process 3: Internal & External Financial Checking");
			row.createCell(12).setCellValue("SLA (no of days)");
			row.createCell(13).setCellValue("Process 4: Decisioning");
			row.createCell(14).setCellValue("SLA (no of days)");
			row.createCell(15).setCellValue("Process 4a: Merchant Acceptance");
			row.createCell(16).setCellValue("SLA (no of days)");
			row.createCell(17).setCellValue("Process 5: Validation & Verification Cardlink Setup");
			row.createCell(18).setCellValue("SLA (no of days)");
			row.createCell(19).setCellValue("Process 5a: Verification with Barcode Update");
			row.createCell(20).setCellValue("SLA (no of days)");
			row.createCell(21).setCellValue("Process 6: Activiate TID & Notify Termnial Provider");
			row.createCell(22).setCellValue("SLA (no of days)");
			row.createCell(23).setCellValue("Process 7: Terminal Installation & Training");
			row.createCell(24).setCellValue("SLA (no of days)");
			row.createCell(25).setCellValue("TAT");
		
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 0, 0));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 1, 1));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 2, 2));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 3, 3));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 4, 4));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 5, 5));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 6, 6));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 7, 7));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 8, 8));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 9, 9));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 10, 10));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 11, 11));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 12, 12));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 13, 13));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 14, 14));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 15, 15));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 16, 16));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 17, 17));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 18, 18));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 19, 19));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 20, 20));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 21, 21));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 22, 22));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 23, 23));
			sheet1.addMergedRegion(new CellRangeAddress(3, 4, 24, 24));
			sheet1.addMergedRegion(new CellRangeAddress(3, 3, 25, 28));

			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
				row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			row = sheet1.createRow(5);
			
			row.createCell(25).setCellValue("Current");
			row.createCell(26).setCellValue("End to end");
			row.createCell(27).setCellValue("Current Exclude 4a: Merchant Acceptance");
			row.createCell(28).setCellValue("Current Exclude 5a: Verification with Barcode Update");

		    for(int i = 25; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			    
			int n = 1;
			int process_1 = 0; double process_1C = 0.0;
			int process_2 = 0; double process_2C = 0.0;
			int process_3 = 0; double process_3C = 0.0;
			int process_4 = 0; double process_4C = 0.0;
			int process_4a = 0; double process_4aC = 0.0;
			int process_5 = 0; double process_5C = 0.0;
			int process_5a = 0; double process_5aC = 0.0;
			int process_6 = 0; double process_6C = 0.0;
			int process_7 = 0; double process_7C = 0.0;
			int current_sla = 0; double current_slaC = 0.0;
			int end_to_end_sla = 0; double end_to_end_slaC = 0.0;
			int current_4a = 0; double current_4aC = 0.0;
			int current_5a = 0; double current_5aC = 0.0;
			int rowNo = 6;
			int colNo = 0;
			
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) rArr){
				if(r.get("process_1") != null) {
					process_1C++;
					process_1 += Integer.parseInt(r.get("process_1"));
				}
				
				if(r.get("process_2") != null) {
					process_2C++;
					process_2 += Integer.parseInt(r.get("process_2"));
				}
				
				if(r.get("process_3") != null) {
					process_3C++;
					process_3 += Integer.parseInt(r.get("process_3"));
				}
				
				if(r.get("process_4") != null) {
					process_4C++;
					process_4 += Integer.parseInt(r.get("process_4"));
				}
				
				if(r.get("process_4a") != null) {
					process_4aC++;
					process_4a += Integer.parseInt(r.get("process_4a"));
				}
				
				if(r.get("process_5") != null) {
					process_5C++;
					process_5 += Integer.parseInt(r.get("process_5"));
				}
				
				if(r.get("process_5a") != null) {
					process_5aC++;
					process_5a += Integer.parseInt(r.get("process_5a"));
				}
				
				if(r.get("process_6") != null) {
					process_6C++;
					process_6 += Integer.parseInt(r.get("process_6"));
				}
				
				if(r.get("process_7") != null) {
					process_7C++;
					process_7 += Integer.parseInt(r.get("process_7"));
				}
				
				if(r.get("current_sla") != null) {
					current_slaC++;
					current_sla += Integer.parseInt(r.get("current_sla"));
				}
				
				if(r.get("end_to_end_sla") != null) {
					end_to_end_slaC++;
					end_to_end_sla += Integer.parseInt(r.get("end_to_end_sla"));
				}
				
				if(r.get("current_4a") != null) {
					current_4aC++;
					current_4a += Integer.parseInt(r.get("current_4a"));
				}
				
				if(r.get("current_5a") != null) {
					current_5aC++;
					current_5a += Integer.parseInt(r.get("current_5a"));
				}
				
				row = sheet1.createRow(rowNo++);
				colNo = 0;
				
				row.createCell(colNo++).setCellValue(n++);
				row.createCell(colNo++).setCellValue(r.get("reference_no") == null ? "-" : r.get("reference_no"));
				row.createCell(colNo++).setCellValue(r.get("type") == null ? "-" : r.get("type"));
				row.createCell(colNo++).setCellValue(r.get("matrix") == null ? "-" : r.get("matrix"));
				row.createCell(colNo++).setCellValue(r.get("group_name") == null ? "-" : r.get("group_name"));
				row.createCell(colNo++).setCellValue(r.get("creator") == null ? "-" : r.get("creator"));
				row.createCell(colNo++).setCellValue(r.get("region") == null ? "-" : r.get("region"));
				row.createCell(colNo++).setCellValue(r.get("status_1") == null ? "-" : r.get("status_1"));
				row.createCell(colNo++).setCellValue(r.get("process_1") == null ? "-" : r.get("process_1"));
				row.createCell(colNo++).setCellValue(r.get("status_2") == null ? "-" : r.get("status_2"));
				row.createCell(colNo++).setCellValue(r.get("process_2") == null ? "-" : r.get("process_2"));
				row.createCell(colNo++).setCellValue(r.get("status_3") == null ? "-" : r.get("status_3"));
				row.createCell(colNo++).setCellValue(r.get("process_3") == null ? "-" : r.get("process_3"));
				row.createCell(colNo++).setCellValue(r.get("status_4") == null ? "-" : r.get("status_4"));
				row.createCell(colNo++).setCellValue(r.get("process_4") == null ? "-" : r.get("process_4"));
				row.createCell(colNo++).setCellValue(r.get("status_4a") == null ? "-" : r.get("status_4a"));
				row.createCell(colNo++).setCellValue(r.get("process_4a") == null ? "-" : r.get("process_4a"));
				row.createCell(colNo++).setCellValue(r.get("status_5") == null ? "-" : r.get("status_5"));
				row.createCell(colNo++).setCellValue(r.get("process_5") == null ? "-" : r.get("process_5"));
				row.createCell(colNo++).setCellValue(r.get("status_5a") == null ? "-" : r.get("status_5a"));
				row.createCell(colNo++).setCellValue(r.get("process_5a") == null ? "-" : r.get("process_5a"));
				row.createCell(colNo++).setCellValue(r.get("status_6") == null ? "-" : r.get("status_6"));
				row.createCell(colNo++).setCellValue(r.get("process_6") == null ? "-" : r.get("process_6"));
				row.createCell(colNo++).setCellValue(r.get("status_7") == null ? "-" : r.get("status_7"));
				row.createCell(colNo++).setCellValue(r.get("process_7") == null ? "-" : r.get("process_7"));
				row.createCell(colNo++).setCellValue(r.get("current_sla") == null ? "-" : r.get("current_sla"));
				row.createCell(colNo++).setCellValue(r.get("end_to_end_sla") == null ? "-" : r.get("end_to_end_sla"));
				row.createCell(colNo++).setCellValue(r.get("current_4a") == null ? "-" : r.get("current_4a"));
				row.createCell(colNo++).setCellValue(r.get("current_5a") == null ? "-" : r.get("current_5a"));
			}
			
			row = sheet1.createRow(rowNo++);
			colNo = 0;
			
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_1C > 0 ? String.format("%.2f", process_1/process_1C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_2C > 0 ? String.format("%.2f", process_2/process_2C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_3C > 0 ? String.format("%.2f", process_3/process_3C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_4C > 0 ? String.format("%.2f", process_4/process_4C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_4aC > 0 ? String.format("%.2f", process_4a/process_4aC) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_5C > 0 ? String.format("%.2f", process_5/process_5C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_5aC > 0 ? String.format("%.2f", process_5a/process_5aC) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_6C > 0 ? String.format("%.2f", process_6/process_6C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_7C > 0 ? String.format("%.2f", process_7/process_7C) : "-"));
			row.createCell(colNo++).setCellValue((current_slaC > 0 ? String.format("%.2f", current_sla/current_slaC) : "-"));
			row.createCell(colNo++).setCellValue((end_to_end_slaC > 0 ? String.format("%.2f", end_to_end_sla/end_to_end_slaC) : "-"));
			row.createCell(colNo++).setCellValue((current_4aC > 0 ? String.format("%.2f", current_4a/current_4aC) : "-"));
			row.createCell(colNo++).setCellValue((current_5aC > 0 ? String.format("%.2f", current_5a/current_5aC) : "-"));
			
			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			
			row.createCell(1).setCellValue("Total Applications");
			row.getCell(1).setCellStyle(style);
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("i) Completed = " + (summary.get("completed") == null ? "-" : summary.get("completed")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue(" - Total Application within SLA = " + (summary.get("completed_1") == null ? "-" : summary.get("completed_1")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue(" - Total Application not within SLA = " + (summary.get("completed_2") == null ? "-" : summary.get("completed_2")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("ii) Work in Progress = " + (summary.get("pending") == null ? "-" : summary.get("pending")));
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("iii) Unsuccessful = " + (summary.get("unsuccessful") == null ? "-" : summary.get("unsuccessful")));
			
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileToDownload.getName() + "\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
	
	/*
	 * 	Get last date, given a year and a month
	 *  Return today's date if the last date is greater than today's date
	 */
	private static String getLastDate(int month, int year) {
	    Calendar calendar = Calendar.getInstance();
	    // passing month-1 because 0-->jan, 1-->feb... 11-->dec
	    calendar.set(year, month - 1, 1);
	    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
	    Date date = calendar.getTime();
	    
	    if(date.compareTo(new Date()) > 0) {
	    	date = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
	    }
	    
	    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	    
	    return sdf.format(date);
	}
	
	public ActionForward region_sla(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.region_sla";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//			if(!isTokenValid(request)){
//				System.out.println("Token validation failed!");
//				session.invalidate();
//				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//			}else{
//				resetToken(request);
//				System.out.println("Token validation passed!");
//			}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="region_sla";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
			
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_user = sanitize(request.getParameter("group_user"));
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));

			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
			
			if(request.getParameter("group_code") != null){
				search.put("group_code", group_code);
			}
			
			if(request.getParameter("group_user") != null){
				search.put("group_user", group_user);
			}
			
			if(request.getParameter("application_type") != null){
				search.put("application_type", application_type);
			}
			
			if(request.getParameter("reseller_location") != null){
				search.put("reseller_location", reseller_location);
			}
		}
		
		System.out.println("Search Filter --> "  + search.toString());
		ArrayList<TreeMap<String, String>> rArr = Report.getRegionSla(search);
		if(rArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		
		ArrayList<TreeMap<String, String>> summary = Report.getRegionSlaSummary(search);
		if(summary == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		
		if(request.getParameter("search_submit") != null){
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			
			if(request.getParameter("month") != null){
				search.put("month", month);
			}
			
			if(request.getParameter("year") != null){
				search.put("year", year);
			}
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "");
				search.put("to_date", "");
			}
		}
		
		request.setAttribute("search", search);
		request.setAttribute("rArr", rArr);
		request.setAttribute("summary", summary);
		
		System.out.println("Result --> " + rArr.toString());
		
		
		ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			if(search.containsKey("group_code") && search.get("group_code").length() > 0){
				userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, search.get("group_code"));
			}
		}
		
		request.setAttribute("userList", userList);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String fileName = "/region_report_" + oSecUserDao.getLogonId() + "_" + sdf.format(new Date()) + ".xls";
			String exportFileName = exportDir + fileName; 
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet1");
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			String month = sanitize(request.getParameter("month"));
			String year = sanitize(request.getParameter("year"));
			String group_code = sanitize(request.getParameter("group_code"));
			String group_name = Report.getGroupName(group_code);
			String group_user = sanitize(request.getParameter("group_user"));
			String group_user_name = Report.getUserName(group_user);
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));
			
			/*----------------------------------------- HEADER --------------------------------------------------*/
		    
		    
			CellStyle style = wb.createCellStyle();//Create style
		    Font font = wb.createFont();//Create font
		    font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
		    style.setFont(font);//set it to bold
		    
		    Row row = sheet1.createRow(0);
			row.createCell(1).setCellValue("Application Type");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(application_type);
			
			row.createCell(6).setCellValue("Group");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_name);
			
			row = sheet1.createRow(1);
			row.createCell(1).setCellValue("Period");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(from_date);
			row.createCell(3).setCellValue("To");
			row.getCell(3).setCellStyle(style);
			row.createCell(4).setCellValue(to_date);
			
			row.createCell(6).setCellValue("User");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(group_user_name);
			
			row= sheet1.createRow(2);
			row.createCell(1).setCellValue("Month");
			row.getCell(1).setCellStyle(style);
			row.createCell(2).setCellValue(month);
			row.createCell(3).setCellValue(year);
			row.createCell(6).setCellValue("Region");
			row.getCell(6).setCellStyle(style);
			row.createCell(7).setCellValue(reseller_location);
		    
			row = sheet1.createRow(3);

			row.createCell(0).setCellValue("No.");
			row.createCell(1).setCellValue("MAP Reference No.");
			row.createCell(2).setCellValue("Application Type");
			row.createCell(3).setCellValue("Group Name");
			row.createCell(4).setCellValue("Creator");
			row.createCell(5).setCellValue("Process 2-5: Validation & Verificaiton Cardlink Setup");
			row.createCell(6).setCellValue("SLA (no of days)");
			row.createCell(7).setCellValue("Process 6: Activate TID & Notify Terminal Provider");
			row.createCell(8).setCellValue("SLA (no of days)");
			row.createCell(9).setCellValue("Process 7: Terminal Installation & Training");
			row.createCell(10).setCellValue("SLA (no of days)");
			row.createCell(11).setCellValue("TAT");
		
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 6, 6));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 7, 7));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 8, 8));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 9, 9));
			sheet1.addMergedRegion(new CellRangeAddress(0, 1, 10, 10));

			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			row = sheet1.createRow(4);
			row.createCell(11).setCellValue("Current");
			row.createCell(12).setCellValue("End to end");

		    for(int i = 11; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			    
			int n = 1;
			int process_1_5 = 0; double process_1_5C = 0.0;
			int process_6 = 0; double process_6C = 0.0;
			int process_7 = 0; double process_7C = 0.0;
			int current_sla = 0; double current_slaC = 0.0;
			int end_to_end_sla = 0; double end_to_end_slaC = 0.0;
			int rowNo = 5;
			int colNo = 0;
			
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) rArr){
				if(r.get("process_1_5") != null) {
					process_1_5C++;
					process_1_5 += Integer.parseInt(r.get("process_1_5"));
				}
				
				if(r.get("process_6") != null) {
					process_6C++;
					process_6 += Integer.parseInt(r.get("process_6"));
				}
				
				if(r.get("process_7") != null) {
					process_7C++;
					process_7 += Integer.parseInt(r.get("process_7"));
				}
				
				if(r.get("current_sla") != null) {
					current_slaC++;
					current_sla += Integer.parseInt(r.get("current_sla"));
				}
				
				if(r.get("end_to_end_sla") != null) {
					end_to_end_slaC++;
					end_to_end_sla += Integer.parseInt(r.get("end_to_end_sla"));
				}
				
				String status = null;
				if(r.get("status_5a") != null) {
					status = r.get("status_5a");
				} else if(r.get("status_5") != null) {
					status = r.get("status_5");
				} else if(r.get("status_4a") != null) {
					status = r.get("status_4a");
				} else if(r.get("status_4") != null) {
					status = r.get("status_4");
				} else if(r.get("status_3") != null) {
					status = r.get("status_3");
				} else if(r.get("status_2") != null) {
					status = r.get("status_2");
				} else if(r.get("status_1") != null) {
					status = r.get("status_1");
				}
				
				row = sheet1.createRow(rowNo++);
				colNo = 0;
				
				row.createCell(colNo++).setCellValue(n++);
				row.createCell(colNo++).setCellValue(r.get("reference_no") == null ? "-" : r.get("reference_no"));
				row.createCell(colNo++).setCellValue(r.get("type") == null ? "-" : r.get("type"));
				row.createCell(colNo++).setCellValue(r.get("group_name") == null ? "-" : r.get("group_name"));
				row.createCell(colNo++).setCellValue(r.get("creator") == null ? "-" : r.get("creator"));
				row.createCell(colNo++).setCellValue(status == null ? "-" : status);
				row.createCell(colNo++).setCellValue(r.get("process_1_5") == null ? "-" : r.get("process_1_5"));
				row.createCell(colNo++).setCellValue(r.get("status_6") == null ? "-" : r.get("status_6"));
				row.createCell(colNo++).setCellValue(r.get("process_6") == null ? "-" : r.get("process_6"));
				row.createCell(colNo++).setCellValue(r.get("status_7") == null ? "-" : r.get("status_7"));
				row.createCell(colNo++).setCellValue(r.get("process_7") == null ? "-" : r.get("process_7"));
				row.createCell(colNo++).setCellValue(r.get("current_sla") == null ? "-" : r.get("current_sla"));
				row.createCell(colNo++).setCellValue(r.get("end_to_end_sla") == null ? "-" : r.get("end_to_end_sla"));
			}
			
			row = sheet1.createRow(rowNo++);
			colNo = 0;
			
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_1_5C > 0 ? String.format("%.2f", process_1_5/process_1_5C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_6C > 0 ? String.format("%.2f", process_6/process_6C) : "-"));
			row.createCell(colNo++).setCellValue("");
			row.createCell(colNo++).setCellValue((process_7C > 0 ? String.format("%.2f", process_7/process_7C) : "-"));
			row.createCell(colNo++).setCellValue((current_slaC > 0 ? String.format("%.2f", current_sla/current_slaC) : "-"));
			row.createCell(colNo++).setCellValue((end_to_end_slaC > 0 ? String.format("%.2f", end_to_end_sla/end_to_end_slaC) : "-"));
			
			for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
		        row.getCell(i).setCellStyle(style);//Set the style
		    }
			
			
			row = sheet1.createRow(rowNo++);
			row = sheet1.createRow(rowNo++);
			
			row.createCell(1).setCellValue("Total Applications");
			row.getCell(1).setCellStyle(style);
			
			int total_count = 0;
			
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) summary){
				total_count += Integer.parseInt(r.get("count"));
				
				row = sheet1.createRow(rowNo++);
				row.createCell(1).setCellValue(r.get("country_code") == null ? "-" : r.get("country_code"));
				row.createCell(2).setCellValue(r.get("count") == null ? "-" : r.get("count"));
			}
			
			row = sheet1.createRow(rowNo++);
			row.createCell(1).setCellValue("Grand Total");
			row.createCell(2).setCellValue(total_count);
			
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileToDownload.getName() + "\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
	
	public ActionForward status_report(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.report_template";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//			if(!isTokenValid(request)){
//				System.out.println("Token validation failed!");
//				session.invalidate();
//				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//			}else{
//				resetToken(request);
//				System.out.println("Token validation passed!");
//			}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="status_report";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		ArrayList<String> statusList = CasemastFactory.getStatusFilter(1);
		if(statusList == null){
			json_error(CasemastFactory.getMessage(), response);
			return null;
		}
		
		request.setAttribute("statusList", statusList);
		
		TreeMap<String, String> search = new TreeMap<String, String>();
			
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String group_code = sanitize(request.getParameter("group_code"));
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));
			String year = sanitize(request.getParameter("year"));
			String month = sanitize(request.getParameter("month"));
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			
			if(request.getParameter("group_code") != null){
				search.put("group_code", group_code);
			}
			
			if(request.getParameter("application_type") != null){
				search.put("application_type", application_type);
			}
			
			if(request.getParameter("reseller_location") != null){
				search.put("reseller_location", reseller_location);
			}
			
			if(request.getParameter("year") != null){
				search.put("year", year);
			}
			
			if(request.getParameter("month") != null){
				search.put("month", month);
			}
			
			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
			
			if(request.getParameter("status") != null){
				search.put("status", request.getParameter("status"));
			}
		}
		else {
			Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
			
			search.put("year", "");
			search.put("month", "");
			search.put("from_date", sdf2.format(dt));
			search.put("to_date", sdf.format(dt));
		}
		
		ArrayList<TreeMap<String, String>> cArr = null;
		if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
			cArr = Report.getStatusReportMonthly(search);
		} else {
			cArr = Report.getStatusReportDaily(search);
		}
		
		if(cArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		
		request.setAttribute("search", search);
		request.setAttribute("cArr", cArr);
		
		/*ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			if(search.containsKey("group_code") && search.get("group_code").length() > 0){
				userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, search.get("group_code"));
			}
		}
		
		request.setAttribute("userList", userList);
		*/
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String exportFileName = exportDir + "/status_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet 1");
			
			int row_index = 0; 
			
			String group_code = sanitize(request.getParameter("group_code"));
			String group_name = Report.getGroupName(group_code);
			String application_type = request.getParameter("application_type");
			String reseller_location = sanitize(request.getParameter("reseller_location"));
			String year = sanitize(request.getParameter("year"));
			String month = sanitize(request.getParameter("month"));
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			
			/* ------------------------------------------------- Header Row --------------------------------------------- */
			CellStyle header_style = wb.createCellStyle();
			Font header_font = wb.createFont();
			header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			header_style.setFont(header_font);
			
			Row condition = sheet1.createRow(row_index);
			condition.createCell(1).setCellValue("Application Type");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(application_type);
					
			condition = sheet1.createRow(++row_index);
			condition.createCell(1).setCellValue("Group");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(group_name);
			
			condition.createCell(4).setCellValue("Region");
			condition.getCell(4).setCellStyle(header_style);
			condition.createCell(5).setCellValue(reseller_location);
			
			condition = sheet1.createRow(++row_index);
			condition.createCell(1).setCellValue("Monthly");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(year);
			condition.createCell(3).setCellValue(month);
			
			condition = sheet1.createRow(++row_index);
			condition.createCell(1).setCellValue("Daily");
			condition.getCell(1).setCellStyle(header_style);
			condition.createCell(2).setCellValue(from_date);
			condition.createCell(3).setCellValue(to_date);
			
			Row header = sheet1.createRow(++row_index);

			String[] header_array = {	"No.", 
										"MAP Reference Number", 
										"Application Type", 
										"Submission Date", 
										"Matrix & Non Matrix Transitions", 
										"Group Name",
										"Creator",
										"Region (Maxis)", 
										"Status", 
										"Duration from Submission to Current Last Status"
									};
			
			Cell column = null;
			for(int i = 0; i < header_array.length; i++) {
				column = header.createCell(i);
				column.setCellValue(header_array[i]);
				column.setCellStyle(header_style);
			}
			
			row_index++;
			/* --------------------------------------------------------------------------------------------------------------*/
			
			
			/* ------------------------------------------------------------ Result Row ------------------------------------- */
			Row row = null;
			
			int n = 0;
			for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
				row = sheet1.createRow(row_index);
				
				String[] result_array = {	Integer.toString(n+1), 
											(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
											(c.get("application_type") != null ? c.get("application_type") : ""), 
											(c.get("created_date") != null ? c.get("created_date") : ""), 
											(c.get("matrix") != null ? c.get("matrix") : ""), 
											(c.get("group_code") != null ? c.get("group_code") : ""), 
											(c.get("creator") != null ? c.get("creator") : ""), 
											(c.get("region") != null ? c.get("region") : ""),
											(c.get("status") != null ? c.get("status") : ""), 
											(c.get("duration") != null ? c.get("duration") : "")
										};
				
				Cell result_column = null;
				for(int i = 0; i < result_array.length; i++) {
					result_column = row.createCell(i);
					result_column.setCellValue(result_array[i]);
				}
				
				row_index++;
				n++;
				
			}
			/* --------------------------------------------------------------------------------------------------------------*/
			
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"Status Report.xls\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
		
		
		return mapping.findForward(nextPage);
	}
	
	public ActionForward decisioning_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="decisioning_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
				
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String group_code = sanitize(request.getParameter("group_code"));
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("group_code") != null){
					search.put("group_code", group_code);
				}
				
				if(request.getParameter("application_type") != null){
					search.put("application_type", application_type);
				}
				
				if(request.getParameter("reseller_location") != null){
					search.put("reseller_location", reseller_location);
				}
				
				if(request.getParameter("year") != null){
					search.put("year", year);
				}
				
				if(request.getParameter("month") != null){
					search.put("month", month);
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("year", "");
				search.put("month", "");
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
			}
			
			ArrayList<TreeMap<String, String>> cArr = null;
			if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
				cArr = Report.getDecisioningReportMonthly(search);
			} else {
				cArr = Report.getDecisioningReportDaily(search);
			}
			
			if(cArr == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/decisioning_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				String group_code = sanitize(request.getParameter("group_code"));
				String group_name = Report.getGroupName(group_code);
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				
				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Application Type");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(application_type);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Group");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(group_name);
				
				condition.createCell(4).setCellValue("Region");
				condition.getCell(4).setCellStyle(header_style);
				condition.createCell(5).setCellValue(reseller_location);
				
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);


				String[] header_array = {	"No.", 
											"MAP Reference Number", 
											"Application Type", 
											"Submission Date", 
											"Matrix", 
											"Group Name",
											"Creator",
											"Region (Maxis)", 
											"Status", 
											"Duration from Pending Decision to Current Last Status"
										};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					String[] result_array = {	Integer.toString(n+1), 
												(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
												(c.get("application_type") != null ? c.get("application_type") : ""), 
												(c.get("created_date") != null ? c.get("created_date") : ""), 
												(c.get("matrix") != null ? c.get("matrix") : ""), 
												(c.get("group_code") != null ? c.get("group_code") : ""), 
												(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("region") != null ? c.get("region") : ""),
												(c.get("status") != null ? c.get("status") : ""), 
												(c.get("duration") != null ? c.get("duration") : "")
											};
					
					Cell result_column = null;
					for(int i = 0; i < result_array.length; i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array[i]);
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"Decisioning Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			
			return mapping.findForward(nextPage);
		}
	
	public ActionForward summary_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.report_template";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		String nextPage = null;
		
		//check session condition
		if (session == null){
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}
//			if(!isTokenValid(request)){
//				System.out.println("Token validation failed!");
//				session.invalidate();
//				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//			}else{
//				resetToken(request);
//				System.out.println("Token validation passed!");
//			}
		
		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			return mapping.findForward(strFwd);
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
		request.setAttribute("groupList", groupList);
		
		
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		
		nextPage="summary_report";
		if(this.authorized(request) == false){
			nextPage="not_authorized";
		}
		
		
		
		TreeMap<String, String> search = new TreeMap<String, String>();
		if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
			String application_type =request.getParameter("application_type");
			String matrix = sanitize(request.getParameter("matrix"));
			String year = sanitize(request.getParameter("year"));
			String month = sanitize(request.getParameter("month"));
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");
			
			if(request.getParameter("application_type") != null){
				search.put("application_type", application_type);
			}
			
			if(request.getParameter("matrix") != null){
				search.put("matrix", matrix);
			}
			
			if(request.getParameter("month") != null && month.length() > 0 && request.getParameter("year") != null &&  year.length() > 0) {
				search.put("month", month);
				search.put("year", year);
				search.put("from_date", "01/" + month + "/" + year);
				search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
			}
			
			if(request.getParameter("from_date") != null){
				search.put("from_date", from_date);
			}
			
			if(request.getParameter("to_date") != null){
				search.put("to_date", to_date);
			}
		}
		else {
			Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
			
			search.put("year", "");
			search.put("month", "");
			search.put("from_date", sdf2.format(dt));
			search.put("to_date", sdf.format(dt));
		}
		
		TreeMap<String, ArrayList<TreeMap<String, String>>> tArr = null;
		
		ArrayList<TreeMap<String, String>> cArr = null;
		ArrayList<TreeMap<String, String>> cSumm = null;
		TreeMap<String, Integer> totalArr1 = null;
		
		if(StringUtils.isNotBlank(request.getParameter("application_type"))){
			cArr = Report.getSummaryReport(search);
			request.setAttribute("cArr", cArr);
		}else{
			tArr = Report.getSummaryReport2(search);
			request.setAttribute("tArr", tArr);
		}
		//cSumm = Report.getSummOfSummaryReport(search);
		
		if(cArr == null && tArr == null){
			json_error(Report.getMessage(), response);
			return null;
		}
		
		if(request.getParameter("search_submit") != null){
			String month = request.getParameter("month") == null ? "" : sanitize(request.getParameter("month"));
			String year = request.getParameter("year") == null ? "" : sanitize(request.getParameter("year"));
			
			if(month.length() > 0 && year.length() > 0) {
				search.put("from_date", "");
				search.put("to_date", "");
			}
		}
		
		request.setAttribute("search", search);
		
		//request.setAttribute("cSumm", cSumm);
		
		
		if(request.getParameter("export_submit") != null){
			try {
				InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
				if (stream != null){
					appCfg.load(stream);
				} else {
					System.out.println("SubmitAppAction Input Stream is NULL");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			
			String exportDir = appCfg.getProperty("Export_Path");
			String exportFileName = exportDir + "/summary_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
			
			Workbook wb = new HSSFWorkbook();
			Sheet sheet1 = wb.createSheet("Sheet 1");
			
			Font bFont = wb.createFont();
			bFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
			CellStyle boldStyle = wb.createCellStyle();
			boldStyle.setFont(bFont);
			
			int row_index = 1; 
			
			/* ------------------------------------------------- Search condition --------------------------------------------- */
			Row search_field = sheet1.createRow(row_index);
			search_field.createCell(2).setCellValue("Application Type :");
			search_field.getCell(2).setCellStyle(boldStyle);
			search_field.createCell(3).setCellValue(search.get("application_type"));
			sheet1.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(row_index, row_index, 3, 4));
			
			search_field.createCell(6).setCellValue("Matrix :");
			search_field.getCell(6).setCellStyle(boldStyle);
			search_field.createCell(7).setCellValue(search.get("matrix"));
			sheet1.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(row_index, row_index, 7, 8));
			row_index++;
			
			search_field = sheet1.createRow(++row_index);
			search_field.createCell(2).setCellValue("Monthly :");
			search_field.getCell(2).setCellStyle(boldStyle);
			search_field.createCell(3).setCellValue(search.get("month"));
			search_field.createCell(4).setCellValue(search.get("year"));
			search_field.createCell(6).setCellValue("Daily :");
			search_field.getCell(6).setCellStyle(boldStyle);
			search_field.createCell(7).setCellValue(search.get("from_date") + " - " + search.get("to_date"));
			sheet1.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(row_index, row_index, 7, 8));
			row_index = row_index+2;
			
			
			/* ------------------------------------------------- Header Row --------------------------------------------- */
			CellStyle header_style = wb.createCellStyle();
			Font header_font = wb.createFont();
			header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			header_style.setFont(header_font);
			header_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			header_style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
			header_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			header_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			header_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
			header_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			header_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
			
			CellStyle header_style1 = wb.createCellStyle(); 
			header_style1.cloneStyleFrom(header_style);
			header_style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			
			
			CellStyle result_style = wb.createCellStyle();
			result_style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			
			CellStyle boldGrey = wb.createCellStyle();
			boldGrey.cloneStyleFrom(boldStyle);
			boldGrey.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
			boldGrey.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			
			CellStyle normalGrey = wb.createCellStyle();
			normalGrey.cloneStyleFrom(result_style);
			normalGrey.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
			normalGrey.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			
			
			TreeMap<String, ArrayList<TreeMap<String, String>>> t = (TreeMap<String, ArrayList<TreeMap<String, String>>>) request.getAttribute("tArr");
			if(t != null && t.size() > 0){
				ArrayList<TreeMap<String, String>> cArrNewAppNonMatrix = t.get("New Application Non Matrix");
				if(cArrNewAppNonMatrix != null && cArrNewAppNonMatrix.size() > 0){
					String type = "Application Type - New (Non Matrix)";
				
					row_index = setSummaryDataToTable(cArrNewAppNonMatrix, sheet1, header_style1, header_style, result_style
							, type, row_index);
					
				}
				
				ArrayList<TreeMap<String, String>> cArrNewAppMatrix = t.get("New Application Matrix");
				if(cArrNewAppMatrix != null && cArrNewAppMatrix.size() > 0){
					String type = "Application Type - New (Matrix)";
					
					row_index = setSummaryDataToTable(cArrNewAppMatrix, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
				
				ArrayList<TreeMap<String, String>> cArrNewAppOther = t.get("New Application Other");
				if(cArrNewAppOther != null && cArrNewAppOther.size() > 0){
					String type = "Application Type - New (Other)";
					
					row_index = setSummaryDataToTable(cArrNewAppOther, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
				
				ArrayList<TreeMap<String, String>> cArrExtend = t.get("Extend Facility");
				if(cArrExtend != null && cArrExtend.size() > 0){
					String type = "Application Type - Extend Facility";
					
					row_index = setSummaryDataToTable(cArrExtend, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
				
				ArrayList<TreeMap<String, String>> cArrECommerce = t.get("e-Commerce/M2u");
				if(cArrECommerce != null && cArrECommerce.size() > 0){
					String type = "Application Type - e-Commerce/M2u";
					
					row_index = setSummaryDataToTable(cArrECommerce, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
				
				ArrayList<TreeMap<String, String>> cArrAddition = t.get("Additional Terminal/mPOS Reader");
				if(cArrAddition != null && cArrAddition.size() > 0){
					String type = "Application Type - Additional Terminal/mPOS Reader";
					
					row_index = setSummaryDataToTable(cArrAddition, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
				
				ArrayList<TreeMap<String, String>> cArrGrand = t.get("Grand Total");
				if(cArrGrand != null && cArrGrand.size() > 0){
					String type = "Application Type - Additional Terminal/mPOS Reader";
					
					row_index = setGrandTotalDataToTable(cArrGrand, sheet1, header_style1, header_style, result_style
							, type, row_index);
				}
			}
			
			ArrayList<TreeMap<String, String>> cArr1 = (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr");
			if(cArr1 != null && cArr1.size() > 0){
				String type = "";
				if(search.containsKey("application_type")){
					type = search.get("application_type");
				} else {
					type = "All";
				}
				String matrix = "";
				if(search.containsKey("matrix")){
					matrix = search.get("matrix");
				} 
				type = ("".equals(type)?"All":type) + ("".equals(matrix)? matrix : ("(" + matrix + ")"));
				row_index = setSummaryDataToTable(cArr1, sheet1, header_style1, header_style, result_style
						, type + matrix, row_index);
			}
		
			/* --------------------------------------------------------------------------------------------------------------*/
			sheet1.autoSizeColumn(0);
			for (int i = 2; i < 14; i++) {
				sheet1.autoSizeColumn(i);
			}
			FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
			wb.write(fileOut);
			fileOut.close();
			wb.close();
			
			
			File fileToDownload = new File(exportFileName);
			FileInputStream inStream = new FileInputStream(fileToDownload);
			
			ServletOutputStream outStream = response.getOutputStream();
			
			response.setContentType("application/octet-stream");
			response.setContentLength((int) fileToDownload.length());
			response.setHeader("Content-Disposition", "attachment; filename=\"Summary Report.xls\"");
			
			byte[] buffer = new byte[4096];
			int bytesRead = -1;
			 
			while ((bytesRead = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
			
			inStream.close();
			outStream.close();
			
			return null;
		}
			
		
		
		
		return mapping.findForward(nextPage);
	}
	
	public ActionForward processing_productivity_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="processing_productivity_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("year") != null){
					search.put("year", year);
				}
				
				if(request.getParameter("month") != null){
					search.put("month", month);
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("year", "");
				search.put("month", "");
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
			}
			
			ArrayList<TreeMap<String, String>> cArr = null;
			if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
				cArr = Report.getProcessingProductivityReportMonthly(search);
			} else {
				cArr = Report.getProcessingProductivityReportDaily(search);
			}
			
			if(cArr == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/processing_productivity_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				
				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);

				/*String[] header_array = {	"Name of Staff", 
											"Total Applications", 
											"Credit Check Results Attached", 
											"Credit Check Complete", 
											"Pre-Update Validation Done", 
											"Request Update Confirmation",
											"Update Done, Request Verification",
											"Verification Done", 
											"Verified, Pending Document", 
											"Total Work Count"
										};*/
				String[] header_array = {
						"Group Name",	
						"Name of Staff",
						"Staff ID", 
						"Total Applications", 
						"Credit Check Result Attached",
						"Request Credit Check",
						"Incomplete Document",
						"Reject - Data Discrepancy",
						"Reject - Document Discrepancy",
						"Reject - Data and Document Discrepancy",
						"Credit Check Incomplete",
						"Request RMS Review (FRAUD)",
						"Recommended by RMS",
						"Not Recommended by RMS",
						"RMS Not Recommended, Request Decision",
						"Pre MID Setup Validation Done",
						"Data Discrepancy",
						"Doc Discrepancy",
						"Data & Doc Discrepancy",
						"Pending Original Doc",
						"Request Final Approval for MID Setup",
						"Pre MID Setup, Validation Done",
						"MID Setup Done. Request Verification",
						"Incomplete Doc. But Proceed for MID Setup",
						"Doc/ Info Provided. Request For MID Setup",
						"Incomplete. To Provide Doc/Info",
						"Amendment Required",
						"Amendment Done",
						"Verified. Filing Complete",
						"Verified. Pending Document",
						"Request For MID Setup",
						"Additional Document Received",
						"Total Work Count"
				};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					/*String[] result_array = {	(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("total_applications") != null ? c.get("total_applications") : ""), 
												(c.get("col_a") != null ? c.get("col_a") : ""), 
												(c.get("col_b") != null ? c.get("col_b") : ""), 
												(c.get("col_c") != null ? c.get("col_c") : ""), 
												(c.get("col_d") != null ? c.get("col_d") : ""), 
												(c.get("col_e") != null ? c.get("col_e") : ""),
												(c.get("col_f") != null ? c.get("col_f") : ""), 
												(c.get("col_g") != null ? c.get("col_g") : ""),
												(c.get("total_work") != null ? c.get("total_work") : "")
											};*/
					List<String> result_array = new ArrayList<String>();
					result_array.add(c.get("role_name") != null ? c.get("role_name") : "");
					result_array.add(c.get("creator_name") != null ? c.get("creator_name") : "");
					result_array.add(c.get("creator") != null ? c.get("creator") : "");
					result_array.add(c.get("total_applications") != null ? c.get("total_applications") : "");
					
					for(int i = 0; i < Report.reportAction.length; i++){
						result_array.add(c.get("col_" + (i + 1)) != null ? c.get("col_" + (i + 1)) : "");
						
					}
					result_array.add(c.get("total_work") != null ? c.get("total_work") : "");
					
					Cell result_column = null;
					for(int i = 0; i < result_array.size(); i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array.get(i));
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"Processing Productivity Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			return mapping.findForward(nextPage);
		}
	
	public ActionForward sales_productivity_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="sales_productivity_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String group_code = sanitize(request.getParameter("group_code"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("group_code") != null){
					search.put("group_code", group_code);
				}
				
				if(request.getParameter("year") != null){
					search.put("year", year);
				}
				
				if(request.getParameter("month") != null){
					search.put("month", month);
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("year", "");
				search.put("month", "");
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
			}
			
			ArrayList<TreeMap<String, String>> cArr = null;
			ArrayList<TreeMap<String, String>> cSumm = null;
			if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
				cArr = Report.getSalesProductivityReportMonthly(search);
				cSumm = Report.getSalesProductivitySummaryMonthly(search);
			} else {
				cArr = Report.getSalesProductivityReportDaily(search);
				cSumm = Report.getSalesProductivitySummaryDaily(search);
			}
			
			if(cArr == null || cSumm == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			request.setAttribute("cSumm", cSumm);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/sales_productivity_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				String group_code = sanitize(request.getParameter("group_code"));
				String group_name = Report.getGroupName(group_code);
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				

				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Group");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(group_name);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);

				String[] header_array = {	"Name of Staff", 
											"Group Name", 
											"No. of Application Submitted", 
											"MCC", 
											"New Application (Matrix)", 
											"New Application (Non Matrix)",
											"Extend Facility",
											"Additional Terminal", 
											"e-Commerce/M2u", 
											"Total",
											"Pending Completion",
											"Closed",
											"Total"
										};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					String[] result_array = {	(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("group_name") != null ? c.get("group_name") : ""), 
												(c.get("total_application") != null ? c.get("total_application") : ""), 
												(c.get("mcc") != null ? c.get("mcc") : ""), 
												(c.get("new_application_matrix") != null ? c.get("new_application_matrix") : ""), 
												(c.get("new_application_non_matrix") != null ? c.get("new_application_non_matrix") : ""), 
												(c.get("extend_facility") != null ? c.get("extend_facility") : ""),
												(c.get("additional_terminal") != null ? c.get("additional_terminal") : ""), 
												(c.get("e_commerce") != null ? c.get("e_commerce") : ""),
												(c.get("total_a") != null ? c.get("total_a") : ""),
												(c.get("pending_completion") != null ? c.get("pending_completion") : ""),
												(c.get("closed") != null ? c.get("closed") : ""),
												(c.get("total_b") != null ? c.get("total_b") : "")
											};
					
					Cell result_column = null;
					for(int i = 0; i < result_array.length; i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array[i]);
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				row_index++; //Spacing
				
				/* ------------------------------------------------- Summary Header Row --------------------------------------------- */
				CellStyle header_style2 = wb.createCellStyle();
				Font header_font2 = wb.createFont();
				header_font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style2.setFont(header_font);
				
				Row header2 = sheet1.createRow(row_index);

				String[] header_array2 = {	"Group Name", 
											"Total"
										};
				
				Cell column2 = null;
				for(int i = 0; i < header_array2.length; i++) {
					column2 = header2.createCell(i);
					column2.setCellValue(header_array2[i]);
					column2.setCellStyle(header_style2);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Summary Result Row ------------------------------------- */
				Row row2 = null;
				
				int grand_total = 0;
				int n2 = 0;
				
				for(TreeMap<String, String> Summ : (ArrayList<TreeMap<String, String>>) request.getAttribute("cSumm")){
					row2 = sheet1.createRow(row_index);
					
					String[] result_array2 = {	(Summ.get("group_name") != null ? Summ.get("group_name") : ""), 
												(Summ.get("total") != null ? Summ.get("total") : "")
											};
					
					Cell result_column2 = null;
					for(int i = 0; i < result_array2.length; i++) {
						result_column2 = row2.createCell(i);
						result_column2.setCellValue(result_array2[i]);
					}
					
					row_index++;
					grand_total = grand_total + Integer.parseInt(Summ.get("total"));
					n2++;
					
				}
				
				Row row3 = null;
				
				row3 = sheet1.createRow(row_index);
				
				String[] result_array3 = {	"GRAND TOTAL", 
											Integer.toString(grand_total)
										};
				
				Cell result_column3 = null;
				for(int i = 0; i < result_array3.length; i++) {
					result_column3 = row3.createCell(i);
					result_column3.setCellValue(result_array3[i]);
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"Sales Productivity Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			return mapping.findForward(nextPage);
		}
	
	public ActionForward closed_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="closed_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
				
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String group_code = sanitize(request.getParameter("group_code"));
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("group_code") != null){
					search.put("group_code", group_code);
				}
				
				if(request.getParameter("application_type") != null){
					search.put("application_type", application_type);
				}
				
				if(request.getParameter("reseller_location") != null){
					search.put("reseller_location", reseller_location);
				}
				
				if(request.getParameter("year") != null){
					search.put("year", year);
				}
				
				if(request.getParameter("month") != null){
					search.put("month", month);
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("year", "");
				search.put("month", "");
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
			}
			
			ArrayList<TreeMap<String, String>> cArr = null;
			ArrayList<TreeMap<String, String>> cSumm = null;
			if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
				cArr = Report.getClosedReportMonthly(search);
				cSumm = Report.getClosedSummaryMonthly(search);
			} else {
				cArr = Report.getClosedReportDaily(search);
				cSumm = Report.getClosedSummaryDaily(search);
			}
			
			if(cArr == null || cSumm == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			request.setAttribute("cSumm", cSumm);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/closed_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				String group_code = sanitize(request.getParameter("group_code"));
				String group_name = Report.getGroupName(group_code);
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				
				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Application Type");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(application_type);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Group");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(group_name);
				
				condition.createCell(4).setCellValue("Region");
				condition.getCell(4).setCellStyle(header_style);
				condition.createCell(5).setCellValue(reseller_location);
				
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);

				String[] header_array = {	"No.", 
											"MAP Reference No.", 
											"Application Type", 
											"Submission Date", 
											"Closed/Rejected Date",
											"Matrix & Non Matrix Transition", 
											"Group Name",
											"Creator",
											"Region (Maxis)", 
											"Status"
										};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					String[] result_array = {	Integer.toString(n+1),
												(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
												(c.get("application_type") != null ? c.get("application_type") : ""), 
												(c.get("created_date") != null ? c.get("created_date") : ""), 
												(c.get("modified_date") != null ? c.get("modified_date") : ""), 
												(c.get("matrix") != null ? c.get("matrix") : ""), 
												(c.get("group_code") != null ? c.get("group_code") : ""), 
												(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("region") != null ? c.get("region") : ""),
												(c.get("status") != null ? c.get("status") : "")
											};
					
					Cell result_column = null;
					for(int i = 0; i < result_array.length; i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array[i]);
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				row_index++; //Spacing
				
				/* ------------------------------------------------- Summary Header Row --------------------------------------------- */
				CellStyle header_style2 = wb.createCellStyle();
				Font header_font2 = wb.createFont();
				header_font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style2.setFont(header_font);
				
				Row header2 = sheet1.createRow(row_index);

				String[] header_array2 = {	
											"Status",
											"Total Applications"
										};
				
				Cell column2 = null;
				for(int i = 0; i < header_array2.length; i++) {
					column2 = header2.createCell(i);
					column2.setCellValue(header_array2[i]);
					column2.setCellStyle(header_style2);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				/* ------------------------------------------------------------ Summary Result Row ------------------------------------- */
				Row row2 = null;
				
				int grand_total = 0;
				int n2 = 0;
				
				for(TreeMap<String, String> Summ : (ArrayList<TreeMap<String, String>>) request.getAttribute("cSumm")){
					row2 = sheet1.createRow(row_index);
					
					String[] result_array2 = {	
												(Summ.get("status") != null ? Summ.get("status") : ""),
												(Summ.get("total") != null ? Summ.get("total") : "")
											};
					
					Cell result_column2 = null;
					for(int i = 0; i < result_array2.length; i++) {
						result_column2 = row2.createCell(i);
						result_column2.setCellValue(result_array2[i]);
					}
					
					row_index++;
					grand_total = grand_total + Integer.parseInt(Summ.get("total"));
					n2++;
					
				}
				
				Row row3 = null;
				
				row3 = sheet1.createRow(row_index);
				
				String[] result_array3 = {	"GRAND TOTAL", 
											Integer.toString(grand_total)
										};
				
				Cell result_column3 = null;
				for(int i = 0; i < result_array3.length; i++) {
					result_column3 = row3.createCell(i);
					result_column3.setCellValue(result_array3[i]);
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"Closed Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			return mapping.findForward(nextPage);
		}
	
	public ActionForward exceptional_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="exceptional_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
				
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String group_code = sanitize(request.getParameter("group_code"));
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("group_code") != null){
					search.put("group_code", group_code);
				}
				
				if(request.getParameter("application_type") != null){
					search.put("application_type", application_type);
				}
				
				if(request.getParameter("reseller_location") != null){
					search.put("reseller_location", reseller_location);
				}
				
				if(request.getParameter("year") != null){
					search.put("year", year);
				}
				
				if(request.getParameter("month") != null){
					search.put("month", month);
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("year", "");
				search.put("month", "");
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
			}
			
			ArrayList<TreeMap<String, String>> cArr = null;
			ArrayList<TreeMap<String, String>> cSumm = null;
			if (request.getParameter("year") != null && !request.getParameter("year").isEmpty() && request.getParameter("month") != null && !request.getParameter("month").isEmpty()){
				cArr = Report.getExceptionalReportMonthly(search);
				cSumm = Report.getExceptionalSummaryMonthly(search);
			} else {
				cArr = Report.getExceptionalReportDaily(search);
				cSumm = Report.getExceptionalSummaryDaily(search);
			}
			
			if(cArr == null || cSumm == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			request.setAttribute("cSumm", cSumm);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/exceptional_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				
				String group_code = sanitize(request.getParameter("group_code"));
				String group_name = Report.getGroupName(group_code);
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Application Type");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(application_type);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Group");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(group_name);
				
				condition.createCell(4).setCellValue("Region");
				condition.getCell(4).setCellStyle(header_style);
				condition.createCell(5).setCellValue(reseller_location);
				
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);

				String[] header_array = {	"No.", 
											"MAP Reference No.", 
											"Application Type", 
											"Submission Date", 
											"Matrix & Non Matrix Transition", 
											"Group Name",
											"Creator",
											"Region (Maxis)", 
											"Status"
										};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					String[] result_array = {	Integer.toString(n+1),
												(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
												(c.get("application_type") != null ? c.get("application_type") : ""), 
												(c.get("created_date") != null ? c.get("created_date") : ""), 
												(c.get("matrix") != null ? c.get("matrix") : ""), 
												(c.get("group_code") != null ? c.get("group_code") : ""), 
												(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("region") != null ? c.get("region") : ""),
												(c.get("status") != null ? c.get("status") : "")
											};
					
					Cell result_column = null;
					for(int i = 0; i < result_array.length; i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array[i]);
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				row_index++; //Spacing
				
				/* ------------------------------------------------- Summary Header Row --------------------------------------------- */
				CellStyle header_style2 = wb.createCellStyle();
				Font header_font2 = wb.createFont();
				header_font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style2.setFont(header_font);
				
				Row header2 = sheet1.createRow(row_index);

				String[] header_array2 = {	"Group Name", 
											"Total"
										};
				
				Cell column2 = null;
				for(int i = 0; i < header_array2.length; i++) {
					column2 = header2.createCell(i);
					column2.setCellValue(header_array2[i]);
					column2.setCellStyle(header_style2);
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Summary Result Row ------------------------------------- */
				Row row2 = null;
				
				int grand_total = 0;
				int n2 = 0;
				
				for(TreeMap<String, String> Summ : (ArrayList<TreeMap<String, String>>) request.getAttribute("cSumm")){
					row2 = sheet1.createRow(row_index);
					
					String[] result_array2 = {	(Summ.get("status") != null ? Summ.get("status") : ""), 
												(Summ.get("total") != null ? Summ.get("total") : "")
											};
					
					Cell result_column2 = null;
					for(int i = 0; i < result_array2.length; i++) {
						result_column2 = row2.createCell(i);
						result_column2.setCellValue(result_array2[i]);
					}
					
					row_index++;
					grand_total = grand_total + Integer.parseInt(Summ.get("total"));
					n2++;
					
				}
				
				Row row3 = null;
				
				row3 = sheet1.createRow(row_index);
				
				String[] result_array3 = {	"GRAND TOTAL", 
											Integer.toString(grand_total)
										};
				
				Cell result_column3 = null;
				for(int i = 0; i < result_array3.length; i++) {
					result_column3 = row3.createCell(i);
					result_column3.setCellValue(result_array3[i]);
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"Exceptional Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			return mapping.findForward(nextPage);
		}
	
	public ActionForward sla_individual_report(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			final String PROG_ID="ReportAction.report_template";

			System.out.println(PROG_ID + " : BEGIN");
			HttpSession session = request.getSession(false);
			
			String nextPage = null;
			
			//check session condition
			if (session == null){
				return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			}
//				if(!isTokenValid(request)){
//					System.out.println("Token validation failed!");
//					session.invalidate();
//					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
//				}else{
//					resetToken(request);
//					System.out.println("Token validation passed!");
//				}
			
			// browser javasript is not enable
			if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
				String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
				session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
				if (DEBUG) System.out.println("... Javascript not enable");
				return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			}
			String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
			SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
			
			String strFwd = "";
			if (oSecUserDao == null){
				strFwd ="sessionError";
			}
			else
			{
				
			}
			if (sessionstatus != null){
				if (DEBUG) System.out.println("sessionstatus !=null");
				if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
					//This is from refresh button.. set session expires.
					if (DEBUG) System.out.println("sessionstatus !=null 2");
					String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
					session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
					return mapping.findForward(Constants.KEY_ACTION_LOGOUT);

				}
			}
			else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
				
				return mapping.findForward(strFwd);
		
			}
			
			
			/********************************************************************************/
			/* Custom code starts here */
			
			ArrayList<SecGroupsDao> groupList = this.myGroupList(request);
			request.setAttribute("groupList", groupList);
			
			
			String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
			
			nextPage="sla_individual_report";
			if(this.authorized(request) == false){
				nextPage="not_authorized";
			}
			
			
			TreeMap<String, String> search = new TreeMap<String, String>();
				
			if(request.getParameter("search_submit") != null || request.getParameter("export_submit") != null){
				String group_code = sanitize(request.getParameter("group_code"));
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String process = sanitize(request.getParameter("process"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				if(request.getParameter("group_code") != null){
					search.put("group_code", group_code);
				}
				
				if(request.getParameter("application_type") != null){
					search.put("application_type", application_type);
				}
				
				if(request.getParameter("reseller_location") != null){
					search.put("reseller_location", reseller_location);
				}
				
				if(request.getParameter("process") != null){
					search.put("process", process);
				}
				
				if(request.getParameter("month") != null && month.length() > 0 && request.getParameter("year") != null &&  year.length() > 0) {
					search.put("month", month);
					search.put("year", year);
					search.put("from_date", "01/" + month + "/" + year);
					search.put("to_date", getLastDate(Integer.parseInt(month), Integer.parseInt(year)));
				}
				
				if(request.getParameter("from_date") != null){
					search.put("from_date", from_date);
				}
				
				if(request.getParameter("to_date") != null){
					search.put("to_date", to_date);
				}
			}
			else {
				Date dt = new Date((new Date()).getTime() - (24 * 60 * 60 * 1000));
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				SimpleDateFormat sdf2 = new SimpleDateFormat("01/MM/yyyy");
				
				search.put("from_date", sdf2.format(dt));
				search.put("to_date", sdf.format(dt));
				search.put("process", "2");
			}
			
			System.out.println(search);
			ArrayList<TreeMap<String, String>> cArr = null;
			
			cArr = Report.getSLAIndividualReport(search);
			if(cArr == null){
				json_error(Report.getMessage(), response);
				return null;
			}
			
			ArrayList<String> statuses = Report.getStatusfromProcess2(search);
			
			
			if(request.getParameter("search_submit") != null){
				String month = request.getParameter("month") == null ? "" : sanitize(request.getParameter("month"));
				String year = request.getParameter("year") == null ? "" : sanitize(request.getParameter("year"));
				
				if(month.length() > 0 && year.length() > 0) {
					search.put("from_date", "");
					search.put("to_date", "");
				}
			}
			
			request.setAttribute("search", search);
			request.setAttribute("cArr", cArr);
			request.setAttribute("statuses", statuses);
			
			if(request.getParameter("export_submit") != null){
				try {
					InputStream stream = ReportAction.class.getResourceAsStream(CONFIG_FILE);
					if (stream != null){
						appCfg.load(stream);
					} else {
						System.out.println("SubmitAppAction Input Stream is NULL");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				
				String exportDir = appCfg.getProperty("Export_Path");
				String exportFileName = exportDir + "/slaindividual_report_" + sdf.format(new Date()) + "_" + oSecUserDao.getLogonId() + ".xls";
				
				Workbook wb = new HSSFWorkbook();
				Sheet sheet1 = wb.createSheet("Sheet 1");
				
				int row_index = 0; 
				
				String group_code = sanitize(request.getParameter("group_code"));
				String group_name = Report.getGroupName(group_code);
				String application_type = request.getParameter("application_type");
				String reseller_location = sanitize(request.getParameter("reseller_location"));
				String process = sanitize(request.getParameter("process"));
				String year = sanitize(request.getParameter("year"));
				String month = sanitize(request.getParameter("month"));
				String from_date = request.getParameter("from_date");
				String to_date = request.getParameter("to_date");
				
				/* ------------------------------------------------- Header Row --------------------------------------------- */
				CellStyle header_style = wb.createCellStyle();
				Font header_font = wb.createFont();
				header_font.setBoldweight(Font.BOLDWEIGHT_BOLD);
				header_style.setFont(header_font);
				

				Row condition = sheet1.createRow(row_index);
				condition.createCell(1).setCellValue("Application Type");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(application_type);
				
				condition.createCell(4).setCellValue("Process");
				condition.getCell(4).setCellStyle(header_style);
				condition.createCell(5).setCellValue(process);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Group");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(group_name);
				
				condition.createCell(4).setCellValue("Region");
				condition.getCell(4).setCellStyle(header_style);
				condition.createCell(5).setCellValue(reseller_location);
				
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Monthly");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(year);
				condition.createCell(3).setCellValue(month);
				
				condition = sheet1.createRow(++row_index);
				condition.createCell(1).setCellValue("Daily");
				condition.getCell(1).setCellStyle(header_style);
				condition.createCell(2).setCellValue(from_date);
				condition.createCell(3).setCellValue(to_date);
				
				Row header = sheet1.createRow(++row_index);
				

				String[] header_array = {	"No.", 
											"MAP Reference No.", 
											"Application Type", 
											"Submission Date", 
											"Matrix & Non Matrix Transition", 
											"Group Name",
											"Creator",
											"Region (Maxis)"
										};
				
				Cell column = null;
				for(int i = 0; i < header_array.length; i++) {
					column = header.createCell(i);
					column.setCellValue(header_array[i]);
					column.setCellStyle(header_style);
				}
				
				int add_i = header_array.length;
				for (String status : statuses){
					column = header.createCell(add_i);
					column.setCellValue(status);
					column.setCellStyle(header_style);
					add_i++;
					
					column = header.createCell(add_i);
					column.setCellValue("Days");
					column.setCellStyle(header_style);
					add_i++;
				}
				
				row_index++;
				/* --------------------------------------------------------------------------------------------------------------*/
				
				
				/* ------------------------------------------------------------ Result Row ------------------------------------- */
				Row row = null;
				
				int n = 0;
				for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
					row = sheet1.createRow(row_index);
					
					String[] result_array = {	Integer.toString(n+1),
												(c.get("map_ref_no") != null ? c.get("map_ref_no") : ""), 
												(c.get("application_type") != null ? c.get("application_type") : ""), 
												(c.get("created_date") != null ? c.get("created_date") : ""), 
												(c.get("matrix") != null ? c.get("matrix") : ""), 
												(c.get("group_code") != null ? c.get("group_code") : ""), 
												(c.get("creator") != null ? c.get("creator") : ""), 
												(c.get("region") != null ? c.get("region") : "")
											};
					
					Cell result_column = null;
					for(int i = 0; i < result_array.length; i++) {
						result_column = row.createCell(i);
						result_column.setCellValue(result_array[i]);
					}
					
					int add_i2 = result_array.length;
					for (String status : statuses){
						if((c.get(status) != null && !"".equals(c.get(status))) || "PUT UP".equals(status)){
							result_column = row.createCell(add_i2);
							result_column.setCellValue(c.get(status + "_s"));
							add_i2++;
						}else{
							result_column = row.createCell(add_i2);
							result_column.setCellValue("");
							add_i2++;
						}
						result_column = row.createCell(add_i2);
						result_column.setCellValue(c.get(status));
						add_i2++;
					}
					
					row_index++;
					n++;
					
				}
				/* --------------------------------------------------------------------------------------------------------------*/
				
				FileOutputStream fileOut = new FileOutputStream(new File(exportFileName));
				wb.write(fileOut);
				fileOut.close();
				wb.close();
				
				
				File fileToDownload = new File(exportFileName);
				FileInputStream inStream = new FileInputStream(fileToDownload);
				
				ServletOutputStream outStream = response.getOutputStream();
				
				response.setContentType("application/octet-stream");
				response.setContentLength((int) fileToDownload.length());
				response.setHeader("Content-Disposition", "attachment; filename=\"SLA by Individual Status Report.xls\"");
				
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				 
				while ((bytesRead = inStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				
				inStream.close();
				outStream.close();
				
				return null;
			}
			
			return mapping.findForward(nextPage);
		}
	
	
	public ActionForward get_group_user(ActionMapping mapping,
		ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		final String PROG_ID="ReportAction.get_group_user";

		System.out.println(PROG_ID + " : BEGIN");
		HttpSession session = request.getSession(false);
		
		//String nextPage = null;
		
		//check session condition
		if (session == null){
			//return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
			json_error("Session not found.", response);
			return null;
		}

		// browser javasript is not enable
		if (request.getParameter(Constants.PARAM_JS_ENABLE) != null && request.getParameter(Constants.PARAM_JS_ENABLE).equals("no") ) {
			String actionMsg = ConfigManager.getInstance().get(Constants.MSG_ERROR_JAVASCRIPT_NOT_ENABLE);
			session.setAttribute(Constants.LOGON_ERR_MSG, actionMsg);
			if (DEBUG) System.out.println("... Javascript not enable");
			//return mapping.findForward(Constants.KEY_ACTION_MAPPING_NOSCRIPT);
			json_error("Javascript not enabled.", response);
			return null;
		}
		String sessionstatus = (String) session.getAttribute(Constants.VISIT_STATUS);
		SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);// read user session time
		
		String strFwd = "";
		if (oSecUserDao == null){
			strFwd ="sessionError";
		}
		else
		{
			
		}
		if (sessionstatus != null){
			if (DEBUG) System.out.println("sessionstatus !=null");
			if ((sessionstatus.equals(Constants.VISIT_STATUS_VISITED)) || (session.isNew())){
				//This is from refresh button.. set session expires.
				if (DEBUG) System.out.println("sessionstatus !=null 2");
				String errMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_LOGOUT_ACTIVE_SESSION);
				session.setAttribute(Constants.LOGON_ERR_MSG, errMsg);
				//return mapping.findForward(Constants.KEY_ACTION_LOGOUT);
				
				json_error("sessionstatus !=null 2", response);
				return null;

			}
		}
		else if (oSecUserDao == null){// if the session expire, it will bring user to timeout screen
			
			//return mapping.findForward(strFwd);
			json_error("Your session has expired", response);
			return null;
	
		}
		
		
		/********************************************************************************/
		/* Custom code starts here */
		
		if(this.authorized(request) == false){
			json_error("Unauthorized access.", response);
			return null;
		}
		
		
		String group_code = sanitize(request.getParameter("group_code"));
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		System.out.println("***** " + strEMerchant);
		ArrayList<SecUserDao> userList = new ArrayList<SecUserDao>();
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			userList = SecUsersHome.getInstance().findSecGroupUserList(strEMerchant, group_code);
		}
		
		
		ArrayList<TreeMap<String, String>> groupUsers = new ArrayList<TreeMap<String, String>>();
		
		for(SecUserDao u : userList){
			TreeMap<String, String> item = new TreeMap<String, String>();
			item.put("id", u.getLogonId());
			item.put("name", u.getUserName());
			
			groupUsers.add(item);
		}
		
		JSONObject obj = new JSONObject();
		obj.put("group_users", groupUsers);
		
		json_ok(response, obj);
		return null;
	}

	private TreeMap<String, Integer> getTotalResult(ArrayList<TreeMap<String, String>> cArr){
		TreeMap<String, Integer> res = new TreeMap<String, Integer>();
		int total_Submission = 0;
		int total_col_c = 0;
		int total_col_d = 0;
		int total_col_e = 0;
		int total_col_f = 0;
		int total_col_g = 0;
		int total_col_h = 0;
		int total_col_i = 0;
		int total_col_j = 0;
		int total_col_k = 0;
		int total_col_l = 0;
		int total_col_m = 0;
		int total_total = 0;
		if (cArr != null) {
			for (TreeMap<String, String> c : cArr) {
				total_Submission += Integer.parseInt(c.get("total_submission"));
				total_col_c += Integer.parseInt(c.get("col_c"));
				total_col_d += Integer.parseInt(c.get("col_d"));
				total_col_e += Integer.parseInt(c.get("col_e"));
				total_col_f += Integer.parseInt(c.get("col_f"));
				total_col_g += Integer.parseInt(c.get("col_g"));
				total_col_h += Integer.parseInt(c.get("col_h"));
				total_col_i += Integer.parseInt(c.get("col_i"));
				total_col_j += Integer.parseInt(c.get("col_j"));
				total_col_k += Integer.parseInt(c.get("col_k"));
				total_col_l += Integer.parseInt(c.get("col_l"));
				total_col_m += Integer.parseInt(c.get("col_m"));
				total_total += Integer.parseInt(c.get("total"));
			}
		}
		res.put("total_submission", total_Submission);
		res.put("col_c", total_col_c);
		res.put("col_d", total_col_d);
		res.put("col_e", total_col_e);
		res.put("col_f", total_col_f);
		res.put("col_g", total_col_g);
		res.put("col_h", total_col_h);
		res.put("col_i", total_col_i);
		res.put("col_j", total_col_j);
		res.put("col_k", total_col_k);
		res.put("col_l", total_col_l);
		res.put("col_m", total_col_m);
		res.put("total", total_total);
		return res;
	}
	
	private int setSummaryDataToTable(ArrayList<TreeMap<String, String>> data, Sheet sheet
			, CellStyle headerStyle, CellStyle headerStyle1, CellStyle resultStyle
			, String type, int rowIndex){
		
		String[] headerArray = {"Group", "Total Submission", type};
		Row header = sheet.createRow(rowIndex);
		
		for(int i = 0; i < 14; i++){
			header.createCell(i).setCellStyle(headerStyle1);
			if(i < headerArray.length){
				header.getCell(i).setCellValue(headerArray[i]);
			}
		}
		sheet.autoSizeColumn(1);
		sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(rowIndex, rowIndex+1, 0, 0));
		sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(rowIndex, rowIndex+1, 1, 1));
		sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(rowIndex, rowIndex, 2, 13));
		
		rowIndex++;
		
		Row header2 = sheet.createRow(rowIndex);
		
		String[] header_array2 = {	"", 
									"", 
									"Pre-Approved", 
									"Document and Credit Check", 
									"Decisioning", 
									"Approve, to issue New Letter of Acceptance",
									"Approved. Request for MID Setup",
									"Decline", 
									"Close-Cancel", 
									"Close-Withdrawn",
									"Pending Verification",
									"Pending Installation",
									"Terminal Installed",
									"Total",
								};
		
		Cell column2 = null;
		for(int i = 0; i < header_array2.length; i++) {
			column2 = header2.createCell(i);
			column2.setCellValue(header_array2[i]);
			column2.setCellStyle(headerStyle);
			
		}
		
		
		rowIndex++;
		
		/* --------------------------------------------------------------------------------------------------------------*/
		
		
		/* ------------------------------------------------------------ Result Row ------------------------------------- */
		Row row = null;
		
		int n = 0;
		
		int sum_TotalSubmission = 0;
		int sum_c = 0;
		int sum_d = 0;
		int sum_e = 0;
		int sum_f = 0;
		int sum_g = 0;
		int sum_h = 0;
		int sum_i = 0;
		int sum_j = 0;
		int sum_k = 0;
		int sum_l = 0;
		int sum_m = 0;
		int sum_TotalWork = 0;
		
		for(TreeMap<String, String> c : data){
			sum_TotalSubmission += c.get("total_submission") != null ?Integer.parseInt(c.get("total_submission")): 0;
			sum_c += c.get("col_c") != null ? Integer.parseInt(c.get("col_c")): 0;
			sum_d += c.get("col_d") != null ? Integer.parseInt(c.get("col_d")): 0;
			sum_e += c.get("col_e") != null ? Integer.parseInt(c.get("col_e")): 0;
			sum_f += c.get("col_f") != null ? Integer.parseInt(c.get("col_f")): 0;
			sum_g += c.get("col_g") != null ? Integer.parseInt(c.get("col_g")): 0;
			sum_h += c.get("col_h") != null ? Integer.parseInt(c.get("col_h")): 0;
			sum_i += c.get("col_i") != null ? Integer.parseInt(c.get("col_i")): 0;
			sum_j += c.get("col_j") != null ? Integer.parseInt(c.get("col_j")): 0;
			sum_k += c.get("col_k") != null ? Integer.parseInt(c.get("col_k")): 0;
			sum_l += c.get("col_l") != null ? Integer.parseInt(c.get("col_l")): 0;
			sum_m += c.get("col_m") != null ? Integer.parseInt(c.get("col_m")): 0;
			sum_TotalWork += c.get("total") != null ? Integer.parseInt(c.get("total")): 0;
			
			row = sheet.createRow(rowIndex);
			
			String[] result_array = {	(c.get("group") != null ? c.get("group") : ""), 
										(c.get("total_submission") != null ? c.get("total_submission") : ""), 
										(c.get("col_c") != null ? c.get("col_c") : ""), 
										(c.get("col_d") != null ? c.get("col_d") : ""), 
										(c.get("col_e") != null ? c.get("col_e") : ""),
										(c.get("col_f") != null ? c.get("col_f") : ""), 
										(c.get("col_g") != null ? c.get("col_g") : ""), 
										(c.get("col_h") != null ? c.get("col_h") : ""),
										(c.get("col_i") != null ? c.get("col_i") : ""), 
										(c.get("col_j") != null ? c.get("col_j") : ""),
										(c.get("col_k") != null ? c.get("col_k") : ""), 
										(c.get("col_l") != null ? c.get("col_l") : ""), 
										(c.get("col_m") != null ? c.get("col_m") : ""), 
										(c.get("total") != null ? c.get("total") : ""), 
									};
			
			Cell result_column = null;
			for(int i = 0; i < result_array.length; i++) {
				result_column = row.createCell(i);
				result_column.setCellValue(result_array[i]);
				if (i != 0) {
					result_column.setCellStyle(resultStyle);
				}
			}
			
			rowIndex++;
			n++;
			
		}
		rowIndex++;
		
		row = sheet.createRow(rowIndex);
		String[] total_array = {"Total"
				,Integer.toString(sum_TotalSubmission)
				, Integer.toString(sum_c)
				, Integer.toString(sum_d)
				, Integer.toString(sum_e)
				, Integer.toString(sum_f)
				, Integer.toString(sum_g)
				, Integer.toString(sum_h)
				, Integer.toString(sum_i)
				, Integer.toString(sum_j)
				, Integer.toString(sum_k)
				, Integer.toString(sum_l)
				, Integer.toString(sum_m)
				, Integer.toString(sum_TotalWork)}; 
		Cell result_column = null;
		for(int i = 0; i < total_array.length; i++) {
			result_column = row.createCell(i);
			result_column.setCellValue(total_array[i]);
			if (i != 0) {
				result_column.setCellStyle(resultStyle);
			}
		}
		
		return rowIndex++;
	}

	private int setGrandTotalDataToTable(ArrayList<TreeMap<String, String>> data, Sheet sheet
			, CellStyle headerStyle, CellStyle headerStyle1, CellStyle resultStyle
			, String type, int rowIndex){
		Row header = sheet.createRow(rowIndex);
		
		String[] header_array = {	"", 
									"Total Submission", 
									"Pre-Approved", 
									"Document and Credit Check", 
									"Decisioning", 
									"Approve, to issue New Letter of Acceptance",
									"Approved. Request for MID Setup",
									"Decline", 
									"Close-Cancel", 
									"Close-Withdrawn",
									"Pending Verification",
									"Pending Installation",
									"Terminal Installed",
									"Total",
								};
		
		Cell column2 = null;
		for(int i = 0; i < header_array.length; i++) {
			column2 = header.createCell(i);
			column2.setCellValue(header_array[i]);
			column2.setCellStyle(headerStyle);
			
		}
		
		rowIndex++;
			
		/* ------------------------------------------------------------ Result Row ------------------------------------- */
		Row row = null;
		
		int n = 0;
		for(TreeMap<String, String> c : data){
			row = sheet.createRow(rowIndex);
			
			String[] result_array = {	"Total",
										(c.get("gTotalSummission") != null ? c.get("gTotalSummission") : ""), 
										(c.get("gTotalCol_C") != null ? c.get("gTotalCol_C") : ""), 
										(c.get("gTotalCol_D") != null ? c.get("gTotalCol_D") : ""), 
										(c.get("gTotalCol_E") != null ? c.get("gTotalCol_E") : ""),
										(c.get("gTotalCol_F") != null ? c.get("gTotalCol_F") : ""), 
										(c.get("gTotalCol_G") != null ? c.get("gTotalCol_G") : ""), 
										(c.get("gTotalCol_H") != null ? c.get("gTotalCol_H") : ""),
										(c.get("gTotalCol_I") != null ? c.get("gTotalCol_I") : ""), 
										(c.get("gTotalCol_J") != null ? c.get("gTotalCol_J") : ""),
										(c.get("gTotalCol_K") != null ? c.get("gTotalCol_K") : ""), 
										(c.get("gTotalCol_L") != null ? c.get("gTotalCol_L") : ""), 
										(c.get("gTotalCol_M") != null ? c.get("gTotalCol_M") : ""), 
										(c.get("gTotalWork") != null ? c.get("gTotalWork") : ""), 
									};
			
			Cell result_column = null;
			for(int i = 0; i < result_array.length; i++) {
				result_column = row.createCell(i);
				result_column.setCellValue(result_array[i]);
				if (i != 0) {
					result_column.setCellStyle(resultStyle);
				}
			}
			
			rowIndex++;
			n++;
			
		}
		return rowIndex++;
	}

}